The following is a list of the most common string functions used. Attached is a working sample of these functions in action.
Q: How do I extract the first number of characters of a string?
A:
Code:
Dim DirectionString As String = "Left Center Right"
DirectionString = DirectionString.Substring(0, 4) 'Get Left
MessageBox.Show(DirectionString)
Q: How do I extract a string from another string?
A:
Code:
Dim DirectionString As String = "Left Center Right"
DirectionString = DirectionString.Substring(5, 6) 'Extract Center
MessageBox.Show(DirectionString)
Q: How do I extract the last few characters of a string?
A:
Code:
Dim DirectionString As String = "Left Center Right"
DirectionString = DirectionString.Substring(12) 'Get Right
MessageBox.Show(DirectionString)
Q: How do I split a string into separate parts?
A:
Code:
Dim SplitString As String = "Milk,Toast,Honey"
Dim SplitArr(2) As String
Dim i As Integer
SplitArr = SplitString.Split(","c) 'Split at Each Comma
For i = 0 To SplitArr.Length - 1
MessageBox.Show(SplitArr(i))
Next
Q: How do I Join strings, with a specified delimiter?
A:
Code:
Dim JoinArr(2) As String
Dim JoinString As String
JoinArr(0) = "Milk"
JoinArr(1) = "Toast"
JoinArr(2) = "Honey"
JoinString = String.Join(",", JoinArr)
MessageBox.Show(JoinString)
Q: How do Insert characters into a string?
A:
Code:
Dim IncompleteString As String = "This is My Stng"
Dim FullString As String
' Results in a value of "This is My String".
FullString = IncompleteString.Insert(13, "ri")
MessageBox.Show(FullString)
Q: How do I remove unwanted characters from a string?
A:
Code:
Dim RemoveString As String = "This is My Str@o@o@ing"
Dim NewString As String
' NewString = "This is My String"
NewString = RemoveString.Remove(14, 5)
MessageBox.Show(NewString)
Q: How do I replace characters with other characters in a string?
A:
Code:
Dim ReplaceString = "This is My String"
Dim NewString As String
' NewString = "This is Another String"
NewString = ReplaceString.Replace("My", "Another")
MessageBox.Show(NewString)
Q: How do I remove leading and trailing spaces?
A:
Code:
'Trim Spcaes
Dim SpaceString As String = " This string will have the spaces removed "
MessageBox.Show(SpaceString)
Dim TrimmedString As String
TrimmedString = Trim(SpaceString)
MessageBox.Show(TrimmedString)
Q: How do I remove leading and trailing characters other than spaces?
A:
Code:
Dim HashString As String = "#####Testing!######"
Dim TrimmedString As String
TrimmedString = HashString.Trim("#")
MessageBox.Show(TrimmedString)
Q: How do I remove only leading spaces?
A:
Code:
Dim SpaceString As String = " This string will have the leading spaces removed "
MessageBox.Show(SpaceString)
Dim TrimmedString As String
TrimmedString = SpaceString.TrimStart(" ")
MessageBox.Show(TrimmedString)
Q: How do I remove only trailing spaces?
A:
Code:
Dim SpaceString As String = " This string will have the trailing spaces removed "
MessageBox.Show(SpaceString)
Dim TrimmedString As String
TrimmedString = SpaceString.TrimEnd(" ")
MessageBox.Show(TrimmedString)
Q: How do I add characters to the beginning of a string?
A:
Code:
Dim MainString As String = "This will be padded on the left"
Dim PaddedString As String
PaddedString = MainString.PadLeft(36, "@") '36 = new length of string with 5 new chars
MessageBox.Show(PaddedString)
Q: How do I add characters to the end of a string?
A:
Code:
Dim MainString As String = "This will be padded on the right"
Dim PaddedString As String
PaddedString = MainString.PadRight(36, "@") '36 = new length of string with 5 new chars
MessageBox.Show(PaddedString)
Q: How do I convert a string to UPPER CASE?
A:
Code:
Dim MixedString As String = "UpPeR oR LoWeR cAsE"
Dim UPPERString As String
' UPPERString = "UPPER OR LOWER CASE"
UPPERString = MixedString.ToUpper
MessageBox.Show(UPPERString)
Q: How do I convert a string to lower case?
A:
Code:
Dim MixedString As String = "UpPeR oR LoWeR cAsE"
Dim lowerString As String
'lowerString = "upper or lower case"
lowerString = MixedString.ToLower
MessageBox.Show(lowerString)
Q: How do I convert a string to Title Case?
A:
Code:
Dim TitleString As String = "UPPER lower Title"
TitleString = TitleString.ToLower() ' mainly because of UPPER not changing to title
TitleString = System.Globalization.CultureInfo.InstalledUICulture.TextInfo.ToTitleCase(TitleString)
MessageBox.Show(TitleString)
Q: How do I use the Format function?
A:
Code:
Dim UnFormattedDateTime As Date = #1/27/2001 5:04:23 PM#
Dim FormatString As String
' Returns current system time in the system-defined long time format.
FormatString = Format(Now(), "Long Time")
' Returns current system date in the system-defined long date format.
MessageBox.Show(FormatString)
FormatString = Format(Now(), "Long Date")
MessageBox.Show(FormatString)
' Also returns current system date in the system-defined long date
' format, using the single letter code for the format.
FormatString = Format(Now(), "D")
MessageBox.Show(FormatString)
' Returns the value of UnFormattedDateTime in user-defined date/time formats.
FormatString = Format(UnFormattedDateTime, "h:m:s") ' Returns "5:4:23".
MessageBox.Show(FormatString)
FormatString = Format(UnFormattedDateTime, "hh:mm:ss tt") ' Returns "05:04:23 PM".
MessageBox.Show(FormatString)
FormatString = Format(UnFormattedDateTime, "dddd, MMM d yyyy") ' Returns "Saturday,
' Jan 27 2001".
MessageBox.Show(FormatString)
FormatString = Format(UnFormattedDateTime, "HH:mm:ss") ' Returns "17:04:23"
MessageBox.Show(FormatString)
FormatString = Format(23) ' Returns "23".
MessageBox.Show(FormatString)
' User-defined numeric formats.
FormatString = Format(5459.4, "##,##0.00") ' Returns "5,459.40".
MessageBox.Show(FormatString)
FormatString = Format(334.9, "###0.00") ' Returns "334.90".
MessageBox.Show(FormatString)
FormatString = Format(5, "0.00%") ' Returns "500.00%".
MessageBox.Show(FormatString)
Q: How do I concatenate strings?
A: Option 1:
Code:
Dim FirstName As String = "Hannes"
Dim Surname As String = "du Preez"
MessageBox.Show(FirstName & Surname)
Option 2:
Code:
Dim ConcatString As String
Dim aString As String = "A"
Dim bString As String = "B"
Dim cString As String = "C"
Dim dString As String = "D"
' ConcatString = "ABCD"
ConcatString = String.Concat(aString, bString, cString, dString)
MessageBox.Show(ConcatString)
Q: How do I determine the position of a certain character in a string?
A:
Code:
Dim MainString As String = "ABCDE"
Dim Result As Integer
Result = MainString.IndexOf("D") ' result = 3
MessageBox.Show("D is Character number : " & Result.ToString)
Q: How do I determine which character is at a certain position?
A:
Code:
Dim MainString As String = "ABCDE"
Dim ResultChar As Char
ResultChar = MainString.Chars(3) ' resultChar = "D"
MessageBox.Show("Character 3 is : " & ResultChar.ToString)
Q: How do I determine the last occurence of a character in a string?
A:
Code:
Dim MainString = "A\B\C\D\E\F\G\H\I\J"
Dim Result As Integer
Result = MainString.LastIndexOf("\")
MessageBox.Show("Last occurence of ' \ ' is at Position : " & Result.ToString())
Q: How do I compare strings?
A:
Code:
Dim FirstString As String = "Alphabetical"
Dim SecondString As String = "Order"
Dim ThirdString As String = "Order"
Dim FourthString As String = "Alphabetical"
Dim Result1 As Integer
Dim Result2 As Integer
Result1 = String.Compare(FirstString, SecondString)
MessageBox.Show("Result = " & Result1.ToString & Environment.NewLine & "First String Is Smaller Than Second")
Result2 = String.Compare(ThirdString, FourthString)
MessageBox.Show("Result = " & Result2.ToString & Environment.NewLine & "Third String Is Greater Than Fourth")
Q: How do I determine the length of a string?
A:
Code:
Dim MainString As String = "String To Test The Length"
Dim Result As Integer = MainString.Length
MessageBox.Show(Result.ToString & " Characters")
Q: How do I copy strings?
A:
Code:
Dim EmptyString As String
Dim CopyString As String = "This Will Be Copied Into An Empty String"
EmptyString = String.Copy(CopyString)
MessageBox.Show(EmptyString)