Part 9 - String Manipulation in VB .Net String Length . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2 Using the string.length property . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2 Removing Characters from a String . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Using TrimStart(), TrimEnd(), Trim() . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Removing Characters from a String . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Using Remove() . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3 3 4 4 Evaluating Strings with StartsWith() and EndWith() . . . . . . . . . . . . . . . . . . . . . . . . . . . 5 ToUpper() and ToLower() Methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6 SubString Manipulation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7 Replace Method . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8 The Mid() Statement . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9 PadLeft() and PadRight() Methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10 Insert Method . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11 IndexOf() Method . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12 Like Operator . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13 1 String Length String Length (Using the string.length property) ! Example 1: Dim strName As String Dim numOfChar As Integer strName = "Smith" numOfChar = strName.Length Console.WriteLine("Name: {0}, Length: {1}", strName, numOfChar) ! Example 2: Dim SSN As String SSN = InputBox("SSN: XXX-XX-XXXX", "SSN") Do While SSN.Length <> 11 SSN = InputBox("SSN: XXX-XX-XXXX", "SSN") Loop 2 Removing Characters from a String ! Using TrimStart(), TrimEnd(), Trim() ! Example: Dim myString As String myString = " Hello " Console.WriteLine("Original String:{0}:", myString) 'Remove leading spaces Console.WriteLine("TrimStart :{0}:", myString.TrimStart()) 'Remove trailing spaces Console.WriteLine("TrimEnd :{0}:", myString.TrimEnd()) 'Remove all spaces Console.WriteLine("Trim :{0}:", myString.Trim()) Original String: Hello TrimStart :Hello TrimEnd : Trim :Hello: : : Hello: Dim myString As String myString = "10.25 % " Console.WriteLine("Original String:{0}:", myString) 'Remove trailing % and spaces Console.WriteLine("TrimEnd :{0}:", myString.TrimEnd("%", " ")) Original String:10.25 % : TrimEnd :10.25: 3 4 Removing Characters from a String ! Using Remove() ! To remove character from anywhere within the string. ! Syntax: String.Remove(StartIndex, Count) ! Example: Dim StrName As String = "John Cober" Dim FName, LName As String Fname = StrName.Remove(4, 6) Lname = StrName.Remove(0, 5) Console.WriteLine("Original String: {0}", StrName) Console.WriteLine("First Name: {0}", FName) Console.WriteLine("Last Name : {0}", LName) 5 Evaluating Strings with StartsWith() and EndWith() ! To determine whether a specific sequence of characters occurs at the beginning or end of a string. ! Syntax: String.StartsWith(SubString) String.EndsWith(SubString) ! Example: (StartsWith) Dim strDownPayment As String = "$2000.00" Console.WriteLine("Original String: {0}", strDownPayment) If strDownPayment.StartsWith("$") Then strDownPayment = strDownPayment.TrimStart("$"c) End If Console.WriteLine("Final String : {0}", strDownPayment) ! Example: (EndsWith) Dim URL As String = "www.iusb.edu" If URL.EndsWith(".edu") Then Console.WriteLine("University Site : {0}", URL) ElseIf URL.EndsWith(".com") Then Console.WriteLine("Company Site : {0}", URL) ElseIf URL.EndsWith(".gov") Then Console.WriteLine("Government Site : {0}", URL) End If 6 ToUpper() and ToLower() Methods ! To access one or more characters contained in a string. ! Syntax: String.ToUpper(String) String.ToLower(String) ! Example: Dim StrName As String = "John Cober" Console.WriteLine("Original String: {0}", StrName) Console.WriteLine("ToLower: {0}", StrName.ToLower()) Console.WriteLine("ToUpper: {0}", StrName.ToUpper()) Output Original String: John Cober ToLower: john cober ToUpper: JOHN COBER 7 SubString Manipulation ! To access one or more characters contained in a string. ! Syntax: String.SubString(StartIndex[, Count]) ! Example: Dim StrName As String = "John Cober" Dim FName, LName As String FName = StrName.Substring(0, 4) LName = StrName.Substring(5) Console.WriteLine("Original String: {0}", StrName) Console.WriteLine("First Name: {0}", FName) Console.WriteLine("Last Name : {0}", LName) Output Original String: John Cober First Name: John Last Name : Cober 8 Replace Method ! To Replace all occurrences of a sequence of characters in a string with another sequence of characters. ! Syntax: String.Replace(OldValue, NewValue) ! Example: Dim strPhone As String = "1-800-111-0000" Dim strNewPhone As String Console.WriteLine("Original String: {0}", strPhone) strNewPhone = strPhone.Replace("800", "877") Console.WriteLine("Change Area Code: {0}", strNewPhone) strNewPhone = strPhone.Replace("-", "") Console.WriteLine("Remove Dashes: {0}", strNewPhone) strNewPhone = strPhone.Replace("1", "2") Console.WriteLine("Change 1's to 2's: {0}", strNewPhone) Output Original String: 1-800-111-0000 Change Area Code: 1-877-111-0000 Remove Dashes: 18001110000 Change 1's to 2's: 2-800-222-0000 9 The Mid() Statement ! To Replace a specific number of characters in a string with characters from another string. ! Syntax: Mid(TargetString, Start [,count]) = ReplacementString ! Example: Dim StrName As String = "John Cober" Console.WriteLine("Original String: {0}", StrName) Mid(StrName, 7, 1) = "yoooooooooooooooo" Console.WriteLine("Change 7th character to 'y': {0}", StrName) Mid(StrName, 6) = "yoooooooooooooooo" Console.WriteLine("Change last name to 'yoooo': {0}", StrName) remaining o's get disregarded Mid(StrName, 1, 4) = "Joshua" 2 character get disregarded Console.WriteLine("Change first name to Josh 'the 'last : {0}", StrName) Output Original String: Change 7th character to 'y': Change last name to 'yoooo': Change first name to Josh : John Cober John Cyber John yoooo Josh yoooo 10 PadLeft() and PadRight() Methods ! Insert characters at the beginning or end of the string. ! Syntax: String.PadLeft(length[,character]) String.PadRight(length[,character]) ‘Begining ‘End ! Example 1: Dim StrName As String = "Hossein" Console.WriteLine("Original String:{0}", StrName) Console.WriteLine("Padleft (within 20 spaces: {0}", StrName.PadLeft(20)) Console.WriteLine("Padleft with dots (within 20 dots) StrName.PadLeft(20, "."c)) : {0}", Console.WriteLine("PadRight with dots (within 20 spaces) : {0}", StrName.PadRight(20, "."c)) Output Original String:Hossein Padleft (within 20 spaces: Hossein Padleft with dots (within 20 dots) : .............Hossein PadRight with dots (within 20 spaces) : Hossein............. 11 ! Example 2: Dim intNum As Integer = 42 Dim strNum As String strNum = intNum.ToString("P") 'Percent strNum = strNum.PadLeft(12, "*"c) Console.WriteLine(strNum) strNum = intNum.ToString("C") 'Currency strNum = strNum.PadLeft(12, "*"c) Console.WriteLine(strNum) Output **4,200.00 % ******$42.00 12 Insert Method ! Inserting characters within a string ! Syntax: String.Insert(StartIndex, Value) ! Example: Dim strName As String = "Bob Smith" Dim StrNew As String StrNew = strname.insert(4, "J. ") Console.WriteLine(StrNew) Output Bob J. Smith 13 IndexOf() Method ! Used to search a string to determine whether it contains a specific sequence of characters. ! Syntax: string.IndexOf(Value [,startIndex]) ! Example: Dim strMessage As String = "Have a Nice Day" Dim intIndex As Integer intIndex = strMessage.IndexOf("Nice") 'return the location for "Nice" Console.WriteLine(intIndex) intIndex = strMessage.IndexOf("Mice") 'return -1 if the text is not found Console.WriteLine(intIndex) intIndex = strMessage.IndexOf("Nice", 8) 'returns -1 because it doesn't find it after the 8th location Console.WriteLine(intIndex) Output 7 -1 -1 14 Like Operator ! Used for pattern matching characters to determine whether one string is equal to another string. ! Syntax: string Like pattern Pattern Matching Characters Matches in String ? Any single character * Zero or more characters # any single digit 0-9 [charlist] any single character in the charlist. [a-z] matches any char that is lower case. [!charlist] any single character not in the charlist. [!a-z] matches any char that is not lower case. ! Example: strName.ToUpper() Like "T[IO]M" 'Matches Tom or Tim MyString Like "###*" 'Matches a string with 3 digits followed by any number of characters 15