VBcoders Guest



Don't have an account yet? Register
 


Forgot Password?



String Functions

by KRYO_11 (20 Submissions)
Category: String Manipulation
Compatability: Visual Basic 5.0
Difficulty: Intermediate
Date Added: Wed 3rd February 2021
Rating: (7 Votes)

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!

Rate String Functions

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

Download this snippet    Add to My Saved Code

String Functions Comments

No comments have been posted about String Functions. Why not be the first to post a comment about String Functions.

Post your comment

Subject:
Message:
0/1000 characters