Important alert: (current site time 7/16/2013 1:56:25 AM EDT)
 

VB icon

_ String Functions _

Email
Submitted on: 12/8/2003 9:08:54 PM
By: KRYO_11  
Level: Intermediate
User Rating: By 7 Users
Compatibility: VB 5.0, VB 6.0
Views: 24982
(About the author)
 
     Includes many common useful string functions. Reverse string, Remove extra spaces, Delimit string, Alternating caps, Proper case, and Count number of occurances of a string in a string. Vote if you like it!
 
code:
Can't Copy and Paste this?
Click here for a copy-and-paste friendly version of this code!
 
Terms of Agreement:   
By using this code, you agree to the following terms...   
  1. You may use this code in your own programs (and may compile it into a program and distribute it in compiled format for languages that allow it) freely and with no charge.
  2. You MAY NOT redistribute this code (for example to a web site) without written permission from the original author. Failure to do so is a violation of copyright laws.   
  3. You may link to this code from another website, but ONLY if it is not wrapped in a frame. 
  4. You will abide by any additional copyright restrictions which the author may have placed in the code or code's description.
				
'**************************************
' Name: _ String Functions _
' Description:Includes many common useful string functions. Reverse string, Remove extra spaces, Delimit string, Alternating caps, Proper case, and Count number of occurances of a string in a string. Vote if you like it!
' By: KRYO_11
'
'This code is copyrighted and has' limited warranties.Please see http://www.Planet-Source-Code.com/vb/scripts/ShowCode.asp?txtCodeId=50381&lngWId=1'for details.'**************************************

Public Function ReverseString(TheString As String) As String
ReverseString = ""
For i = 0 To Len(TheString) - 1
ReverseString = ReverseString & Mid(TheString, Len(TheString) - i, 1)
Next i
End Function
Public Function RemoveExtraSpaces(TheString As String) As String
Dim LastChar As String
Dim NextChar As String
LastChar = Left(TheString, 1)
RemoveExtraSpaces = LastChar
For i = 2 To Len(TheString)
NextChar = Mid(TheString, i, 1)
If NextChar = " " And LastChar = " " Then
Else
RemoveExtraSpaces = RemoveExtraSpaces & NextChar
End If
LastChar = NextChar
Next i
End Function
Public Function DelimitString(TheString As String, Delimiter As String) As String
DelimitString = ""
For i = 1 To Len(TheString)
If i <> Len(TheString) Then
DelimitString = DelimitString & Mid(TheString, i, 1) & Delimiter
Else
DelimitString = DelimitString & Mid(TheString, i, 1)
End If
Next i
End Function
Public Function AltCaps(TheString As String, Optional StartWithFirstCharacter As Boolean = True) As String
Dim LastCap As Boolean
AltCaps = ""
If StartWithFirstCharacter = False Then LastCap = True
For i = 1 To Len(TheString)
If LastCap = False Then
AltCaps = AltCaps & UCase(Mid(TheString, i, 1))
LastCap = True
Else
AltCaps = AltCaps & LCase(Mid(TheString, i, 1))
LastCap = False
End If
Next i
End Function
Public Function Propercase(TheString As String) As String
Propercase = UCase(Left(TheString, 1))
For i = 2 To Len(TheString)
If Mid(TheString, i - 1, 1) = " " Then
Propercase = Propercase & UCase(Mid(TheString, i, 1))
Else
Propercase = Propercase & LCase(Mid(TheString, i, 1))
End If
Next i
End Function
Public Function CountCharacters(TheString As String, CharactersToCheckFor As String) As Integer
 Dim Char As String
 Dim ReturnAgain As Boolean
 CountCharacters = 0
 For i = 1 To Len(TheString)
If i < (Len(TheString) + 1 - Len(CharactersToCheckFor)) Then
Char = Mid(TheString, i, Len(CharactersToCheckFor))
ReturnAgain = True
Else
Char = Mid(TheString, i)
ReturnAgain = False
End If
If Char = CharactersToCheckFor Then CountCharacters = CountCharacters + 1
If ReturnAgain = False Then GoTo NextPos
Next i
NextPos:
End Function


Other 37 submission(s) by this author

 


Report Bad Submission
Use this form to tell us if this entry should be deleted (i.e contains no code, is a virus, etc.).
This submission should be removed because:

Your Vote

What do you think of this code (in the Intermediate category)?
(The code with your highest vote will win this month's coding contest!)
Excellent  Good  Average  Below Average  Poor (See voting log ...)
 

Other User Comments

12/8/2003 10:09:40 PMCodeClub

good work!
(If this comment was disrespectful, please report it.)

 
12/8/2003 10:10:42 PMCodeClub

but there is room for improvement such as Single Entry/Single Exit code standard;)
(If this comment was disrespectful, please report it.)

 
12/9/2003 4:10:02 AMIan Webling

I uploaded some modifications to your code that you may be interested in looking at.
(If this comment was disrespectful, please report it.)

 
12/9/2003 1:47:42 PM

good work
(If this comment was disrespectful, please report it.)

 
12/10/2003 5:09:25 AMFrank

Good work !
(If this comment was disrespectful, please report it.)

 
5/10/2004 2:04:06 AM

very nice...!
what if i wan format this string to date format?

20040522 to 22/05/2004
pls email me at carmen622@hotmail.com

(If this comment was disrespectful, please report it.)

 
6/25/2004 10:12:46 PMCraigénbök

Excellent code mate, it was exactly what i was looking for.
You've managed to do what took me about 40 lines, with only 8!
(If this comment was disrespectful, please report it.)

 
8/31/2004 9:33:29 AM

Thanks for that code, it helped me out with a nasty little string counting problem.

Nice work
(If this comment was disrespectful, please report it.)

 
10/14/2004 11:32:13 PMMoe Figit

Try this, also shows how to remove certain characters from a string as well.

Do While InStr(1, strTest, " ")
strTest = Left(strTest, InStr(1, strTest, " ") - 1) + Right(strTest, Len(strTest) - InStr(1, strTest, " "))
nCountSpace = nCountSpace + 1
Loop

I know it won't post right but I hold you get the idea.
(If this comment was disrespectful, please report it.)

 
12/24/2004 9:42:25 AM

hi! i wud just like to ask if do u knw how to generate a crossword puzzle maker? i rily do hav a problem bwt it. cud u help me? thanx and more power... :)

-storm_lyz@yahoo.com
(If this comment was disrespectful, please report it.)

 
12/24/2004 9:44:32 AM

hi! i wud like to ask if do u knw how to generate a crossword puzzle maker? users jst hav 2 input the words nd the clues nd wen it s submmited it will automatically make a crosword puzzle.. i rily do hav a problem bwt it. can u help me? thanx and more power... :)

-storm_lyz@yahoo.com
(If this comment was disrespectful, please report it.)

 
2/20/2005 7:40:51 AMcarniv0re

Very slow... but good job anyway...
(If this comment was disrespectful, please report it.)

 
2/24/2005 8:36:06 PMSpiritman

I've rated it 4 in the Intermediate category, but I've posted some suggested improvements here:
http://www.planet-source-code.com/vb/scripts/ShowCode.asp?txtCodeId=59128&lngWI d=1
You
may be interested in them!
(If this comment was disrespectful, please report it.)

 
2/28/2005 10:29:12 PM

great code......would you by any chance know how to convert text that a user entered into proper grammer....ex if they enter
(If this comment was disrespectful, please report it.)

 
9/17/2005 1:22:46 AMKuangda He

(\,,,/)
(0.o) < Great Work > <(^.^)>
(> <)
(If this comment was disrespectful, please report it.)

 
5/17/2007 9:56:49 PMJacky

Good Work!
(If this comment was disrespectful, please report it.)

 
7/14/2007 10:44:01 PMknormalnight

I used the Proper Case function as a basis for a redesigned version, which would use any non-alphanumerical character as a spot to make the next character capitalized.

Private Function ProperCase(InputString As String) As String

Dim I As Long

ProperCase = UCase(Left(InputString, 1))

For I = 2 To Len(InputString)
If (Asc(UCase$(Mid$(InputString, I - 1, 1))) < Asc("A")) Or (Asc(UCase$(Mid$(InputString, I - 1, 1))) > Asc("Z")) Then
ProperCase = ProperCase & UCase(Mid(InputString, I, 1))
Else
ProperCase = ProperCase & LCase(Mid(InputString, I, 1))
End If
Next I

End Function

It's a bit hefty, but works well for small sets of text, like "ABC", "[ABC]", or "ABC (ABC),ABC 2.ABC", or other unusual sets of seperators and letters.
(If this comment was disrespectful, please report it.)

 

Add Your Feedback
Your feedback will be posted below and an email sent to the author. Please remember that the author was kind enough to share this with you, so any criticisms must be stated politely, or they will be deleted. (For feedback not related to this particular code, please click here instead.)
 

To post feedback, first please login.