Search Tools Links Login

_ String Functions _


Visual Basic 6, or VB Classic

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!

Original Author: KRYO_11

Code

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

About this post

Posted: 2003-06-01
By: ArchiveBot
Viewed: 113 times

Categories

Visual Basic 6

Attachments

No attachments for this post


Loading Comments ...

Comments

No comments have been added for this post.

You must be logged in to make a comment.