1 / 22

Chapter 15 – Strings, Characters and Regular Expressions

Chapter 15 – Strings, Characters and Regular Expressions.

lamya
Download Presentation

Chapter 15 – Strings, Characters and Regular Expressions

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Chapter 15 – Strings, Characters and Regular Expressions Outline15.1 Introduction15.2   Fundamentals of Characters and Strings15.3   String Constructors15.4   StringLength and Chars Properties and CopyTo Method15.5   Comparing Strings15.6   String Method GetHashCode15.7   Locating Characters and Substrings in Strings15.8   Extracting Substrings from Strings15.9   Concatenating Strings15.10   Miscellaneous String Methods15.11   ClassStringBuilder15.12   StringBuilder Indexer, Length and CapacityProperties, and EnsureCapacity Method15.13StringBuilderAppend and AppendFormat Methods15.14StringBuilderInsert, Remove and Replace Methods15.15 Char Methods15.16Card Shuffling and Dealing Simulation15.17Regular Expressions and Class Regex

  2. 15.1 Introduction • String and character processing • Useful in a variety of applications • String and Char classes (System) • General string and character processing, storage • StringBuilder class (System.Text) • Facilitates efficient construction of strings • Regex and Match classes (System.Text.RegularExpressions) • Powerful pattern matching capabilities

  3. 15.2   Fundamentals of Characters and Strings • Characters • Fundamental building blocks of source code • Character constants • Represented using double quotes and c character • All characters correspond to an integer character code • Unicode character set • ChrW function converts Unicode values to characters

  4. 15.2   Fundamentals of Characters and Strings • Strings • A series of characters treated as a single unit • String literals • Objects of class String • Upcoming example: String constructors

  5. ChrW converts Unicode value 34 to double quote character, " Creates an array of type Char.Suffix c required when using OptionStrict Creates a literal String object Creates a new String object containing a copy of characters in characterArray string1 and originalString reference same literal String object Copies characters from characterArray, starting at the index indicated by second argument and continuing for the number of characters indicated by the third argument Creates a String with length indicated by the second argument, filled with copies of the character passed as the first argument Each instance of variable quotes represents a double quote character, " 1 ' Fig. 15.1: StringConstructor.vb 2 ' Demonstrating String class constructors. 3 4 Imports System.Windows.Forms 5 6 Module modStringConstructor 7 8 Sub Main() 9 Dim characterArray AsChar() 10 Dim output AsString 11Dim quotes AsString = ChrW(34) 12 Dim originalString, string1, string2, string3, _ 13 string4 AsString 14 15 characterArray = NewChar() {"b"c, "i"c, "r"c, _ 16 "t"c, "h"c, " "c, "d"c, "a"c, "y"c} 17 18 ' string initialization 19originalString ="Welcome to VB.NET Programming!" 20 string1 = originalString 21 string2 = New String(characterArray) 22 string3 = New String(characterArray, 6, 3) 23 string4 = New String("C"c, 5) 24 25 output = "string1 = " & quotes & string1 & quotes & _ 26 vbCrLf & "string2 = " & quotes & string2 & quotes & _ 27 vbCrLf & "string3 = " & quotes & string3 & quotes & _ 28 vbCrLf & "string4 = " & quotes & string4 & quotes 29 30 MessageBox.Show(output, "String Class Constructors", _ 31 MessageBoxButtons.OK, MessageBoxIcon.Information) 32 EndSub' Main 33 34 EndModule' modStringConstructor StringConstructor.vbChrW functionCalls to String constructors

  6. StringConstructor.vb

  7. Length property returns number of characters in string Copies characters from a string into an array.Respectively, arguments represent: the location at which to begin copying, the destination array, the index into which to place the first character copied and the number of characters to copy Length property used to loop backwards through characters in string1 Returns the character at the position indicated by the integer argument 1 ' Fig. 15.2: StringMiscellaneous.vb 2 ' Using properties Length and Chars 3 ' of class string. 4 5 Imports System.Windows.Forms 6 7 Module modMiscellaneous 8 9 Sub Main() 10 Dim string1, output AsString 11 Dim characterArray AsChar() 12 Dim i AsInteger 13 Dim quotes AsString = ChrW(34) 14 15 string1 = "hello there" 16 characterArray = NewChar(5) {} 17 18 ' output string 19 output = "string1: " & quotes & string1 & quotes 20 21 ' test Length property 22 output &= vbCrLf & "Length of string1: " & string1.Length 23 24 ' loop through characters in string1 and display 25 ' reversed 26 output &= vbCrLf & "The string reversed is: " 27 28For i = string1.Length - 1To0Step-1 29 output &= string1.Chars(i) 30 Next 31 32 ' copy characters from string1 into characterArray 33 string1.CopyTo(0, characterArray, 0, 5) 34 output &= vbCrLf & "The character array is: " 35 StringMiscellaneous.vb

  8. Displays contents of characterArray 36For i = 0To characterArray.Length - 1 37 output &= characterArray(i) 38 Next 39 40 MessageBox.Show(output, "Demonstrating String" & _ 41 " properties Length and Chars", _ 42 MessageBoxButtons.OK, MessageBoxIcon.Information) 43 EndSub' Main 44 45 EndModule' modMiscellaneous StringMiscellaneous.vb

  9. 15.5   Comparing Strings • Lexicographical comparison • Similar to alphabetization • Each character corresponds to a number • Character codes compared from beginning of string • Methods Equals, CompareTo and = operator • Reference comparison • Determines whether two references contain the same object • Isoperator • Upcoming example: String test to determine equality

  10. Method Equals performs case-sensitive lexicographical comparison Equality operator produces same results as method Equals 1 ' Fig. 15.3: StringCompare.vb 2 ' Comparing strings. 3 4 Imports System.Windows.Forms 5 6 Module modCompare 7 8 Sub Main() 9 Dim string1 AsString = "hello" 10 Dim string2 AsString = "good bye" 11 Dim string3 AsString = "Happy Birthday" 12 Dim string4 AsString = "happy birthday" 13 Dim output AsString 14 Dim quotes AsString = ChrW(34) 15 16 ' output values of four Strings 17 output = "string1 = " & quotes & string1 & quotes & _ 18 vbCrLf & "string2 = " & quotes & string2 & quotes& _ 19 vbCrLf & "string3 = " & quotes & string3 & quotes& _ 20 vbCrLf & "string4 = " & quotes & string4 & quotes& _ 21 vbCrLf & vbCrLf 22 23 ' test for equality using Equals method 24If (string1.Equals("hello")) Then 25 output &= "string1 equals " & quotes & "hello" & _ 26 quotes & vbCrLf 27 28 Else 29 output &= "string1 does not equal " & quotes & _ 30 "hello" & quotes & vbCrLf 31 EndIf 32 33 ' test for equality with = 34If string1 = "hello"Then 35 output &= "string1 equals " & quotes & "hello" & _ StringCompare.vbCall to Equals= operator

  11. Shared method Equals compares two Strings lexicographically Method CompareTo performs a lexicographical comparison.Returns 0 if Strings are equal.Returns –1 if argument String is greater.Returns 1 if calling String is greater 36 quotes & vbCrLf 37 Else 38 output &= "string1 does not equal " & quotes & _ 39 "hello" & quotes & vbCrLf 40 EndIf 41 42 ' test for equality comparing case 43If (String.Equals(string3, string4)) Then 44 output &= "string3 equals string4" & vbCrLf 45 Else 46 output &= "string3 does not equal string4" & vbCrLf 47 EndIf 48 49 ' test CompareTo 50 output &= vbCrLf & "string1.CompareTo(string2) is " & _ 51 string1.CompareTo(string2) & vbCrLf & _ 52 "string2.CompareTo(string1) is " & _ 53 string2.CompareTo(string1) & vbCrLf & _ 54 "string1.CompareTo(string1) is " & _ 55 string1.CompareTo(string1) & vbCrLf & _ 56 "string3.CompareTo(string4) is " & _ 57 string3.CompareTo(string4) & vbCrLf & _ 58 "string4.CompareTo(string3) is " & _ 59 string4.CompareTo(string3) & vbCrLf & vbCrLf 60 61 MessageBox.Show(output, "Demonstrating string" & _ 62 " comparisons", MessageBoxButtons.OK, _ 63 MessageBoxIcon.Information) 64 EndSub' Main 65 66 EndModule ' modCompare StringCompare.vbCall to EqualsCall to CompareTo

  12. StringCompare.vb

  13. Method StartsWith determines whether the beginning of a String matches the String passed as an argument Method EndsWith determines whether the end of a String matches the String passed as an argument 1 ' Fig. 15.4: StringStartEnd.vb 2 ' Demonstrating StartsWith and EndsWith methods. 3 4 Imports System.Windows.Forms 5 6 Module modStartEnd 7 8 Sub Main() 9 Dim strings AsString() 10 Dim output AsString = "" 11 Dim i AsInteger 12 Dim quotes AsString = ChrW(34) 13 14 strings = NewString() {"started", "starting", _ 15 "ended", "ending"} 16 17 ' test every string to see if it starts with "st" 18 For i = 0To strings.GetUpperBound(0) 19 20If strings(i).StartsWith("st") Then 21 output &= quotes & strings(i) &quotes & _ 22 " starts with " & quotes & "st" & quotes & vbCrLf 23 EndIf 24 25 Next 26 27 output &= vbCrLf 28 29 ' test every string to see if it ends with "ed" 30 For i = 0 To strings.GetUpperBound(0) 31 32If strings(i).EndsWith("ed") Then 33 output &= quotes & strings(i) & quotes & _ 34 " ends with " & quotes & "ed" & quotes & vbCrLf StringStartEnd.vbCall to StartsWithCall to EndsWith

  14. 35 EndIf 36 37 Next 38 39 MessageBox.Show(output, "Demonstrating StartsWith and" & _ 40 " EndsWith methods", MessageBoxButtons.OK, _ 41 MessageBoxIcon.Information) 42 EndSub ' Main 43 44 EndModule' modStartEnd StringStartEnd.vb

  15. Alternative toChrW(34).Two consecutive double quotation marks ("") produces one double quotation mark in the String Finds first occurrence of c in Stringletters Finds first occurrence of a in letters, starting at position 1 Searches for occurrence of $ in letters starting at position 3 and searching for 5 characters.Returns –1, indicating there is no occurrence Finds last occurrence of c Finds last occurrence of $ searching back from position 15 for 5 characters.Returns -1 Finds last occurrence of a by searching back from position 25 1 ' Fig. 15.6: StringIndexMethods.vb 2 ' Using String searching methods. 3 4 Imports System.Windows.Forms 5 6 Module modIndexMethods 7 8 SubMain() 9 Dim letters AsString = "abcdefghijklmabcdefghijklm" 10 Dim output AsString 11 Dim searchLetters AsChar() = NewChar() {"c"c, "a"c, "$"c} 12 13 ' test IndexOf to locate a character in a string 14 output &= """c"" is located at index " & _ 15 letters.IndexOf("c"c) 16 17 output &= vbCrLf & """a"" is located at index " & _ 18 letters.IndexOf("a"c, 1) 19 20 output &= vbCrLf & """$"" is located at index " & _ 21 letters.IndexOf("$"c, 3, 5) 22 23 ' test LastIndexOf to find a character in a string 24 output &= vbCrLf & vbCrLf & "Last ""c"" is located at " & _ 25"index " & letters.LastIndexOf("c"c) 26 27 output &= vbCrLf & "Last ""a"" is located at index " & _ 28 letters.LastIndexOf("a"c, 25) 29 30 output &= vbCrLf & "Last ""$"" is located at index " & _ 31 letters.LastIndexOf("$"c, 15, 5) 32 StringIndexMethods.vbCalls to IndexOfCalls to LastIndexOf

  16. Whereas previous calls to IndexOf searched for a character, this call finds the substring "def" Searches for "def" starting at position 7 Searches for "hello" starting at position 5, continuing for 15 characters Searches from end of String to find last occurrence of "def" Searches back from position 25 Searches back from position 20 for 15 characters Searches for first occurrence of any character in searchLetters array Searches for first occurrence of a character in searchLetters, starting at position 7 Searches for 5 characters starting at position 20 33 ' test IndexOf to locate a substring in a string 34 output &= vbCrLf & vbCrLf & """def"" is located at" & _ 35 " index " & letters.IndexOf("def") 36 37 output &= vbCrLf & """def"" is located at index " & _ 38 letters.IndexOf("def", 7) 39 40 output &= vbCrLf & """hello"" is located at index " & _ 41 letters.IndexOf("hello", 5, 15) 42 43 ' test LastIndexOf to find a substring in a string 44 output &= vbCrLf & vbCrLf & "Last ""def"" is located " & _ 45"at index " & letters.LastIndexOf("def") 46 47 output &= vbCrLf & "Last ""def"" is located at " & _ 48 letters.LastIndexOf("def", 25) 49 50 output &= vbCrLf & "Last ""hello"" is located at " & _ 51"index " & letters.LastIndexOf("hello", 20, 15) 52 53 ' test IndexOfAny to find first occurrence of character 54 ' in array 55 output &= vbCrLf & vbCrLf & "First occurrence of ""c""," & _ 56 " ""a""or ""$"" is located at " & _ 57 letters.IndexOfAny(searchLetters) 58 59 output &= vbCrLf & "First occurrence of ""c"", ""a"" or " & _ 60 """$"" is located at " & _ 61 letters.IndexOfAny(searchLetters, 7) 62 63 output &= vbCrLf & "First occurrence of ""c"", ""a"" or " & _ 64 """$"" is located at " & _ 65 letters.IndexOfAny(searchLetters, 20, 5) 66 StringIndexMethods.vbCalls to IndexOfCalls to LastIndexOfCalls to IndexOfAny

  17. Searches for last occurrence of any in an array of characters Searches back from position 1 Searches back from position 25 for 5 characters 67 ' test LastIndexOfAny to find first occurrence of character 68 ' in array 69 output &= vbCrLf & vbCrLf & "Last occurrence of ""c""," & _ 70 " ""a"" or ""$"" is located at " & _ 71 letters.LastIndexOfAny(searchLetters) 72 73 output &= vbCrLf & "Last occurrence of ""c"", ""a"" or " & _ 74 """$"" is located at " & _ 75 letters.LastIndexOfAny(searchLetters, 1) 76 77 output &= vbCrLf & "Last occurrence of ""c"", ""a"" or " & _ 78 """$""is located at " & _ 79 letters.LastIndexOfAny(searchLetters, 25, 5) 80 81 MessageBox.Show(output, _ 82 "Demonstrating String class index methods") 83 EndSub' Main 84 85 EndModule' modIndexMethods StringIndexMethods.vbCalls to LastIndexOfAny

  18. StringIndexMethods.vb

  19. Method Substring returns a new String object generated by copying characters from the calling String.One argument version returns characters between position indicated and end of String Two argument version returns substring starting at position indicated by first argument with length indicated by second argument 1 ' Fig. 15.7: SubString.vb 2 ' Demonstrating the String Substring method. 3 4 Imports System.Windows.Forms 5 6 Module modSubString 7 8 Sub Main() 9 Dim letters As String = "abcdefghijklmabcdefghijklm" 10 Dim output As String 11 Dim quotes AsString = ChrW(34) 12 13 ' invoke SubString method and pass it one parameter 14 output = "Substring from index 20 to end is " & _ 15quotes & letters.Substring(20) & quotes & vbCrLf 16 17 ' invoke SubString method and pass it two parameters 18 output &= "Substring from index 0 to 6 is " &_ 19 quotes& letters.Substring(0, 6) & quotes 20 21 MessageBox.Show(output, _ 22 "Demonstrating String method Substring") 23 End Sub ' Main 24 25 End Module ' modSubString SubString.vbCall to SubstringCall to Substring

  20. Shared method Concat returns a new String object containing the combined characters from both original Strings 1 ' Fig. 15.8: SubConcatination.vb 2 ' Demonstrating String class ConCat method. 3 4 Imports System.Windows.Forms 5 6 Module modSubConcat 7 8 Sub Main() 9 Dim string1 As String = "Happy " 10 Dim string2 As String = "Birthday" 11 Dim output As String 12 13 output = "string1 = """ &string1 & """" & _ 14 vbCrLf & "string2 = """ & string2 & """" 15 16 output &= vbCrLf & vbCrLf & _ 17 "Result of String.Concat(string1, string2) = " & _ 18String.Concat(string1, string2) 19 20 MessageBox.Show(output, _ 21 "Demonstrating String method Concat") 22 End Sub ' Main 23 24 End Module ' modSubConcat SubConcatination.vbCall to Concat

  21. Method Replace replaces every instance of the character indicated by the first argument with the second argument Method ToUpper creates a new Stringwith all lowercase characters converted to uppercase Converts all uppercase characters to lowercase 1 ' Fig. 15.9: StringMiscellaneous.vb 2 ' Demonstrating String methods Replace, ToLower, ToUpper, Trim, 3 ' and ToString. 4 5 Imports System.Windows.Forms 6 7 Module modStringMiscellaneous 8 9 Sub Main() 10 Dim string1 As String = "cheers!" 11 Dim string2 As String = "GOOD BYE " 12 Dim string3 As String = " spaces " 13 Dim output As String 14 Dim quotes AsString = ChrW(34) 15 Dim i As Integer 16 17 output = "string1 = " & quotes & string1 & quotes & _ 18 vbCrLf & "string2 = " & quotes & string2 & quotes & _ 19 vbCrLf & "string3 = " & quotes & string3 & quotes 20 21 ' call method Replace 22 output &= vbCrLf & vbCrLf & "Replacing " & quotes & "e" & _ 23 quotes & " with " & quotes & "E" & quotes & _ 24" in string1: " & quotes & string1.Replace("e"c, "E"c) & _ 25 quotes 26 27 ' call ToLower and ToUpper 28 output &= vbCrLf & vbCrLf & "string1.ToUpper() = " & _ 29 quotes & string1.ToUpper() & quotes & vbCrLf & _ 30"string2.ToLower() = " & quotes & string2.ToLower() & _ 31 quotes 32 StringMiscellaneous.vbCall to ReplaceCall to ToUpper Call to ToLower

  22. Method Trim returns a copy of the calling String with leading and trailing whitespace characters removed Method ToString is provided for class String because String is derived from class Object 33 ' call Trim method 34 output &= vbCrLf & vbCrLf & "string3 after trim = " & _ 35 quotes & string3.Trim() & quotes 36 37 ' call ToString method 38 output &= vbCrLf & vbCrLf & "string1 = " & _ 39 quotes & string1.ToString() & quotes 40 41 MessageBox.Show(output, _ 42 "Demonstrating Miscellaneous String Methods") 43 End Sub ' Main 44 45 End Module ' modStringMiscellaneous StringMiscellaneous.vbCall to TrimCall to ToString

More Related