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:
Q: How do I extract a string from another string?Code:Dim DirectionString As String = "Left Center Right" DirectionString = DirectionString.Substring(0, 4) 'Get Left MessageBox.Show(DirectionString)
A:
Q: How do I extract the last few characters of a string?Code:Dim DirectionString As String = "Left Center Right" DirectionString = DirectionString.Substring(5, 6) 'Extract Center MessageBox.Show(DirectionString)
A:
Q: How do I split a string into separate parts?Code:Dim DirectionString As String = "Left Center Right" DirectionString = DirectionString.Substring(12) 'Get Right MessageBox.Show(DirectionString)
A:
Q: How do I Join strings, with a specified delimiter?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
A:
Q: How do Insert characters into a string?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)
A:
Q: How do I remove unwanted characters from a string?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)
A:
Q: How do I replace characters with other characters in a string?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)
A:
Q: How do I remove leading and trailing spaces?Code:Dim ReplaceString = "This is My String" Dim NewString As String ' NewString = "This is Another String" NewString = ReplaceString.Replace("My", "Another") MessageBox.Show(NewString)
A:
Q: How do I remove leading and trailing characters other than spaces?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)
A:
Q: How do I remove only leading spaces?Code:Dim HashString As String = "#####Testing!######" Dim TrimmedString As String TrimmedString = HashString.Trim("#") MessageBox.Show(TrimmedString)
A:
Q: How do I remove only trailing spaces?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)
A:
Q: How do I add characters to the beginning of a string?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)
A:
Q: How do I add characters to the end of a string?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)
A:
Q: How do I convert a string to UPPER CASE?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)
A:
Q: How do I convert a string to lower case?Code:Dim MixedString As String = "UpPeR oR LoWeR cAsE" Dim UPPERString As String ' UPPERString = "UPPER OR LOWER CASE" UPPERString = MixedString.ToUpper MessageBox.Show(UPPERString)
A:
Q: How do I convert a string to Title Case?Code:Dim MixedString As String = "UpPeR oR LoWeR cAsE" Dim lowerString As String 'lowerString = "upper or lower case" lowerString = MixedString.ToLower MessageBox.Show(lowerString)
A:
Q: How do I use the Format function?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)
A:
Q: How do I concatenate strings?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)
A: Option 1:
Option 2:Code:Dim FirstName As String = "Hannes" Dim Surname As String = "du Preez" MessageBox.Show(FirstName & Surname)
Q: How do I determine the position of a certain character in a string?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)
A:
Q: How do I determine which character is at a certain position?Code:Dim MainString As String = "ABCDE" Dim Result As Integer Result = MainString.IndexOf("D") ' result = 3 MessageBox.Show("D is Character number : " & Result.ToString)
A:
Q: How do I determine the last occurence of a character in a string?Code:Dim MainString As String = "ABCDE" Dim ResultChar As Char ResultChar = MainString.Chars(3) ' resultChar = "D" MessageBox.Show("Character 3 is : " & ResultChar.ToString)
A:
Q: How do I compare strings?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())
A:
Q: How do I determine the length of a string?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")
A:
Q: How do I copy strings?Code:Dim MainString As String = "String To Test The Length" Dim Result As Integer = MainString.Length MessageBox.Show(Result.ToString & " Characters")
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)


Reply With Quote
Bookmarks