chapter 15 – strings, characters and regular expressions

67
2002 Prentice Hall. All rights reserved. 1 Chapter 15 – Strings, Characters and Regular Expressions Outline 15.1 Introduction 15.2 Fundamentals of Characters and Strings 15.3 String Constructors 15.4 String Length and Chars Properties and CopyTo Method 15.5 Comparing Strings 15.6 String Method GetHashCode 15.7 Locating Characters and Substrings in Strings 15.8 Extracting Substrings from Strings 15.9 Concatenating Strings 15.10 Miscellaneous String Methods 15.11 Class StringBuilder 15.12 StringBuilder Indexer, Length and Capacity Properties, and EnsureCapacity Method 15.13 StringBuilder Append and AppendFormat Methods 15.14 StringBuilder Insert, Remove and Replace Methods 15.15 Char Methods 15.16 Card Shuffling and Dealing Simulation

Upload: magnar

Post on 20-Jan-2016

48 views

Category:

Documents


0 download

DESCRIPTION

Chapter 15 – Strings, Characters and Regular Expressions. - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: Chapter 15 – Strings, Characters and Regular Expressions

2002 Prentice Hall. All rights reserved.

1

Chapter 15 – Strings, Characters and Regular Expressions

Outline15.1 Introduction15.2   Fundamentals of Characters and Strings15.3   String Constructors15.4   String Length 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  Class StringBuilder15.12  StringBuilder Indexer, Length and Capacity Properties, and EnsureCapacity Method15.13  StringBuilder Append and AppendFormat Methods

15.14   StringBuilder Insert, Remove and Replace Methods15.15 Char Methods15.16   Card Shuffling and Dealing Simulation15.17  Regular Expressions and Class Regex

Page 2: Chapter 15 – Strings, Characters and Regular Expressions

2002 Prentice Hall. All rights reserved.

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

Page 3: Chapter 15 – Strings, Characters and Regular Expressions

2002 Prentice Hall. All rights reserved.

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

Page 4: Chapter 15 – Strings, Characters and Regular Expressions

2002 Prentice Hall. All rights reserved.

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

Page 5: Chapter 15 – Strings, Characters and Regular Expressions

2002 Prentice Hall.All rights reserved.

Outline5

StringConstructor.vb

ChrW function

Calls to String constructors

1 ' Fig. 15.1: StringConstructor.vb2 ' Demonstrating String class constructors.3 4 Imports System.Windows.Forms5 6 Module modStringConstructor7 8 Sub Main()9 Dim characterArray As Char()10 Dim output As String11 Dim quotes As String = ChrW(34)12 Dim originalString, string1, string2, string3, _13 string4 As String14 15 characterArray = New Char() {"b"c, "i"c, "r"c, _16 "t"c, "h"c, " "c, "d"c, "a"c, "y"c}17 18 ' string initialization19 originalString = "Welcome to VB.NET Programming!"20 string1 = originalString21 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 & quotes29 30 MessageBox.Show(output, "String Class Constructors", _31 MessageBoxButtons.OK, MessageBoxIcon.Information)32 End Sub ' Main33 34 End Module ' modStringConstructor

ChrW converts Unicode value 34 to double quote character, "

Creates an array of type Char.Suffix c required when using Option Strict

Creates a literal String object

string1 and originalString reference same literal String object

Creates a new String object containing a copy of characters in characterArray

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, "

Page 6: Chapter 15 – Strings, Characters and Regular Expressions

2002 Prentice Hall.All rights reserved.

Outline6

StringConstructor.vb

Page 7: Chapter 15 – Strings, Characters and Regular Expressions

2002 Prentice Hall.All rights reserved.

Outline7

StringMiscellaneous.vb

1 ' Fig. 15.2: StringMiscellaneous.vb2 ' Using properties Length and Chars3 ' of class string.4 5 Imports System.Windows.Forms6 7 Module modMiscellaneous8 9 Sub Main()10 Dim string1, output As String11 Dim characterArray As Char()12 Dim i As Integer13 Dim quotes As String = ChrW(34)14 15 string1 = "hello there"16 characterArray = New Char(5) {}17 18 ' output string19 output = "string1: " & quotes & string1 & quotes20 21 ' test Length property22 output &= vbCrLf & "Length of string1: " & string1.Length23 24 ' loop through characters in string1 and display 25 ' reversed26 output &= vbCrLf & "The string reversed is: "27 28 For i = string1.Length - 1 To 0 Step -129 output &= string1.Chars(i)30 Next31 32 ' copy characters from string1 into characterArray33 string1.CopyTo(0, characterArray, 0, 5)34 output &= vbCrLf & "The character array is: "35

Length property returns number of characters in string

Length property used to loop backwards through characters in string1

Returns the character at the position indicated by the integer argument

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

Page 8: Chapter 15 – Strings, Characters and Regular Expressions

2002 Prentice Hall.All rights reserved.

Outline8

StringMiscellaneous.vb

36 For i = 0 To characterArray.Length - 137 output &= characterArray(i)38 Next39 40 MessageBox.Show(output, "Demonstrating String" & _41 " properties Length and Chars", _42 MessageBoxButtons.OK, MessageBoxIcon.Information)43 End Sub ' Main44 45 End Module ' modMiscellaneous

Displays contents of characterArray

Page 9: Chapter 15 – Strings, Characters and Regular Expressions

2002 Prentice Hall. All rights reserved.

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– Is operator

– Upcoming example: String test to determine equality

Page 10: Chapter 15 – Strings, Characters and Regular Expressions

2002 Prentice Hall.All rights reserved.

Outline10

StringCompare.vb

Call to Equals

= operator

1 ' Fig. 15.3: StringCompare.vb2 ' Comparing strings.3 4 Imports System.Windows.Forms5 6 Module modCompare7 8 Sub Main()9 Dim string1 As String = "hello"10 Dim string2 As String = "good bye"11 Dim string3 As String = "Happy Birthday"12 Dim string4 As String = "happy birthday"13 Dim output As String14 Dim quotes As String = ChrW(34)15 16 ' output values of four Strings17 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 & vbCrLf22 23 ' test for equality using Equals method24 If (string1.Equals("hello")) Then25 output &= "string1 equals " & quotes & "hello" & _26 quotes & vbCrLf2728 Else29 output &= "string1 does not equal " & quotes & _30 "hello" & quotes & vbCrLf31 End If32 33 ' test for equality with =34 If string1 = "hello" Then35 output &= "string1 equals " & quotes & "hello" & _

Method Equals performs case-sensitive lexicographical comparison

Equality operator produces same results as method Equals

Page 11: Chapter 15 – Strings, Characters and Regular Expressions

2002 Prentice Hall.All rights reserved.

Outline11

StringCompare.vb

Call to Equals

Call to CompareTo

36 quotes & vbCrLf37 Else38 output &= "string1 does not equal " & quotes & _39 "hello" & quotes & vbCrLf40 End If41 42 ' test for equality comparing case43 If (String.Equals(string3, string4)) Then44 output &= "string3 equals string4" & vbCrLf45 Else46 output &= "string3 does not equal string4" & vbCrLf47 End If48 49 ' test CompareTo50 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 & vbCrLf60 61 MessageBox.Show(output, "Demonstrating string" & _62 " comparisons", MessageBoxButtons.OK, _63 MessageBoxIcon.Information)64 End Sub ' Main65 66 End Module ' modCompare

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

Page 12: Chapter 15 – Strings, Characters and Regular Expressions

2002 Prentice Hall.All rights reserved.

Outline12

StringCompare.vb

Page 13: Chapter 15 – Strings, Characters and Regular Expressions

2002 Prentice Hall.All rights reserved.

Outline13

StringStartEnd.vb

Call to StartsWith

Call to EndsWith

1 ' Fig. 15.4: StringStartEnd.vb2 ' Demonstrating StartsWith and EndsWith methods.3 4 Imports System.Windows.Forms5 6 Module modStartEnd7 8 Sub Main()9 Dim strings As String()10 Dim output As String = ""11 Dim i As Integer12 Dim quotes As String = ChrW(34)13 14 strings = New String() {"started", "starting", _15 "ended", "ending"}16 17 ' test every string to see if it starts with "st"18 For i = 0 To strings.GetUpperBound(0)19 20 If strings(i).StartsWith("st") Then21 output &= quotes & strings(i) & quotes & _22 " starts with " & quotes & "st" & quotes & vbCrLf23 End If24 25 Next26 27 output &= vbCrLf28 29 ' test every string to see if it ends with "ed"30 For i = 0 To strings.GetUpperBound(0)31 32 If strings(i).EndsWith("ed") Then33 output &= quotes & strings(i) & quotes & _34 " ends with " & quotes & "ed" & quotes & vbCrLf

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

Page 14: Chapter 15 – Strings, Characters and Regular Expressions

2002 Prentice Hall.All rights reserved.

Outline14

StringStartEnd.vb

35 End If36 37 Next38 39 MessageBox.Show(output, "Demonstrating StartsWith and" & _40 " EndsWith methods", MessageBoxButtons.OK, _41 MessageBoxIcon.Information)42 End Sub ' Main43 44 End Module ' modStartEnd

Page 15: Chapter 15 – Strings, Characters and Regular Expressions

2002 Prentice Hall. All rights reserved.

15

15.6  String Method GetHashCode

• Hash tables– Store data so that it can be retrieved efficiently

– Complex data types, such as Strings, can be used to look up data in hash table

– Hash codes• Facilitate logical storage and retrieval of data items

• Every possible data item corresponds to a number (hash code)

• Code is obtained using a special calculation (hash function)

– Produces identical hash codes for identical data

– Should distribute data evenly among hash codes

– Defined in Overridable method GetHashCode

– Upcoming example: GetHashCode method demonstration

Page 16: Chapter 15 – Strings, Characters and Regular Expressions

2002 Prentice Hall.All rights reserved.

Outline16

StringHashCode.vb

Call to GetHashCode

Call to GetHashCode

1 ' Fig. 15.5: StringHashCode.vb2 ' Demonstrating method GetHashCode of class String.3 4 Imports System.Windows.Forms5 6 Module modHashCode7 8 Sub Main()9 Dim string1 As String = "hello"10 Dim string2 As String = "Hello"11 Dim output As String12 Dim quotes As String = ChrW(34)13 14 output = "The hash code for " & quotes & string1 & _15 quotes & " is " & string1.GetHashCode() & vbCrLf16 17 output &= "The has code for " & quotes & string2 & _18 quotes & " is " & string2.GetHashCode()19 20 MessageBox.Show(output, _21 "Demonstrating String Method GetHashCode")22 End Sub ' Main23 24 End Module ' modHashCode

Strings to be hashed differ only in case of first letter

Calling GetHashCode on any String containing "hello" always produces hash code 178056679

Despite similarity, to "hello", "Hello" produces code 222703111

Page 17: Chapter 15 – Strings, Characters and Regular Expressions

2002 Prentice Hall.All rights reserved.

Outline17

StringIndexMethods.vb

Calls to IndexOf

Calls to LastIndexOf

1 ' Fig. 15.6: StringIndexMethods.vb2 ' Using String searching methods.3 4 Imports System.Windows.Forms5 6 Module modIndexMethods7 8 Sub Main()9 Dim letters As String = "abcdefghijklmabcdefghijklm"10 Dim output As String 11 Dim searchLetters As Char() = New Char() {"c"c, "a"c, "$"c}12 13 ' test IndexOf to locate a character in a string14 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 string24 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

Alternative to ChrW(34).Two consecutive double quotation marks ("") produces one double quotation mark in the String

Finds first occurrence of c in String letters

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 a by searching back from position 25

Finds last occurrence of $ searching back from position 15 for 5 characters.Returns -1

Page 18: Chapter 15 – Strings, Characters and Regular Expressions

2002 Prentice Hall.All rights reserved.

Outline18

StringIndexMethods.vb

Calls to IndexOf

Calls to LastIndexOf

Calls to IndexOfAny

33 ' test IndexOf to locate a substring in a string34 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 string44 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 array55 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

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

Page 19: Chapter 15 – Strings, Characters and Regular Expressions

2002 Prentice Hall.All rights reserved.

Outline19

StringIndexMethods.vb

Calls to LastIndexOfAny

67 ' test LastIndexOfAny to find first occurrence of character 68 ' in array69 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 End Sub ' Main84 85 End Module ' modIndexMethods

Searches for last occurrence of any in an array of characters

Searches back from position 1

Searches back from position 25 for 5 characters

Page 20: Chapter 15 – Strings, Characters and Regular Expressions

2002 Prentice Hall.All rights reserved.

Outline20

StringIndexMethods.vb

Page 21: Chapter 15 – Strings, Characters and Regular Expressions

2002 Prentice Hall.All rights reserved.

Outline21

SubString.vb

Call to Substring

Call to Substring

1 ' Fig. 15.7: SubString.vb2 ' Demonstrating the String Substring method.3 4 Imports System.Windows.Forms5 6 Module modSubString7 8 Sub Main()9 Dim letters As String = "abcdefghijklmabcdefghijklm"10 Dim output As String 11 Dim quotes As String = ChrW(34)12 13 ' invoke SubString method and pass it one parameter14 output = "Substring from index 20 to end is " & _15 quotes & letters.Substring(20) & quotes & vbCrLf16 17 ' invoke SubString method and pass it two parameters18 output &= "Substring from index 0 to 6 is " & _19 quotes & letters.Substring(0, 6) & quotes20 21 MessageBox.Show(output, _22 "Demonstrating String method Substring")23 End Sub ' Main24 25 End Module ' modSubString

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

Page 22: Chapter 15 – Strings, Characters and Regular Expressions

2002 Prentice Hall.All rights reserved.

Outline22

SubConcatination.vb

Call to Concat

1 ' Fig. 15.8: SubConcatination.vb2 ' Demonstrating String class ConCat method.3 4 Imports System.Windows.Forms5 6 Module modSubConcat7 8 Sub Main()9 Dim string1 As String = "Happy "10 Dim string2 As String = "Birthday"11 Dim output As String12 13 output = "string1 = """ & string1 & """" & _14 vbCrLf & "string2 = """ & string2 & """"15 16 output &= vbCrLf & vbCrLf & _17 "Result of String.Concat(string1, string2) = " & _18 String.Concat(string1, string2)19 20 MessageBox.Show(output, _21 "Demonstrating String method Concat")22 End Sub ' Main23 24 End Module ' modSubConcat

Shared method Concat returns a new String object containing the combined characters from both original Strings

Page 23: Chapter 15 – Strings, Characters and Regular Expressions

2002 Prentice Hall.All rights reserved.

Outline23

StringMiscellaneous.vb

Call to Replace

Call to ToUpper Call to ToLower

1 ' Fig. 15.9: StringMiscellaneous.vb2 ' Demonstrating String methods Replace, ToLower, ToUpper, Trim, 3 ' and ToString.4 5 Imports System.Windows.Forms6 7 Module modStringMiscellaneous8 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 String14 Dim quotes As String = ChrW(34)15 Dim i As Integer16 17 output = "string1 = " & quotes & string1 & quotes & _18 vbCrLf & "string2 = " & quotes & string2 & quotes & _19 vbCrLf & "string3 = " & quotes & string3 & quotes20 21 ' call method Replace22 output &= vbCrLf & vbCrLf & "Replacing " & quotes & "e" & _23 quotes & " with " & quotes & "E" & quotes & _24 " in string1: " & quotes & string1.Replace("e"c, "E"c) & _25 quotes26 27 ' call ToLower and ToUpper28 output &= vbCrLf & vbCrLf & "string1.ToUpper() = " & _29 quotes & string1.ToUpper() & quotes & vbCrLf & _30 "string2.ToLower() = " & quotes & string2.ToLower() & _31 quotes32

Method Replace replaces every instance of the character indicated by the first argument with the second argument

Method ToUpper creates a new String with all lowercase characters converted to uppercase

Converts all uppercase characters to lowercase

Page 24: Chapter 15 – Strings, Characters and Regular Expressions

2002 Prentice Hall.All rights reserved.

Outline24

StringMiscellaneous.vb

Call to Trim

Call to ToString

33 ' call Trim method34 output &= vbCrLf & vbCrLf & "string3 after trim = " & _35 quotes & string3.Trim() & quotes36 37 ' call ToString method38 output &= vbCrLf & vbCrLf & "string1 = " & _39 quotes & string1.ToString() & quotes40 41 MessageBox.Show(output, _42 "Demonstrating Miscellaneous String Methods")43 End Sub ' Main44 45 End Module ' modStringMiscellaneous

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

Page 25: Chapter 15 – Strings, Characters and Regular Expressions

2002 Prentice Hall. All rights reserved.

25

15.11  Class StringBuilder

• Disadvantages of class String– Contents of a String object can not be modified after

creation

– Concatenation and most String operations require creation of a new String object

• Class StringBuilder– System.Text namespace

– Stores a modifiable string of characters

– Has a set capacity that can be increased when necessary

– More efficient than String if string is modified repeatedly

– Less efficient than String if string is fixed

– Upcoming example: StringBuilder class constructors

Page 26: Chapter 15 – Strings, Characters and Regular Expressions

2002 Prentice Hall.All rights reserved.

Outline26

StringBuilderConstructor.vb

Calls to constructors

1 ' Fig. 15.10: StringBuilderConstructor.vb2 ' Demonstrating StringBuilder class constructors.3 4 Imports System.Text5 Imports System.Windows.Forms6 7 Module modBuilderConstructor8 9 Sub Main()10 Dim buffer1, buffer2, buffer3 As StringBuilder11 Dim quotes As String = ChrW(34)12 Dim output As String13 14 buffer1 = New StringBuilder()15 buffer2 = New StringBuilder(10)16 buffer3 = New StringBuilder("hello")17 18 output = "buffer1 = " & quotes & buffer1.ToString() & _19 quotes & vbCrLf20 21 output &= "buffer2 = " & quotes & _22 buffer2.ToString() & quotes & vbCrLf 23 24 output &= "buffer3 = " & quotes & _25 buffer3.ToString() & quotes26 27 MessageBox.Show(output, _28 "Demonstrating StringBuilder Class Constructors")29 End Sub ' Main30 31 End Module ' modBuilderConstructor

Creates a StringBuilder with no characters and default initial capacity of 16 characters

Creates empty StringBuilder with capacity 10

Creates StringBuilder containing contents of String argument.Initial capacity is smallest power of two greater than the number of characters in argument String

Must call method ToString to use concatenation operator

Page 27: Chapter 15 – Strings, Characters and Regular Expressions

2002 Prentice Hall.All rights reserved.

Outline27

StringBuilderConstructor.vb

Page 28: Chapter 15 – Strings, Characters and Regular Expressions

2002 Prentice Hall. All rights reserved.

2815.12  StringBuilder Indexer, Length and Capacity Properties, and

EnsureCapacity Method• StringBuilder capacities

– Number of characters that can be stored without allocating more memory

– Returned by Capacity property– Length property returns number of characters stored

– Method EnsureCapacity• Guarantees capacity greater than specified value

• New capacity determined by:

– Doubling current capacity if large enough, or

– Making new capacity one larger than capacity requested

– Upcoming example: StringBuilder size manipulation

Page 29: Chapter 15 – Strings, Characters and Regular Expressions

2002 Prentice Hall.All rights reserved.

Outline29

StringBuilderFeatures.vb

Length propertyCapacity property

Call to EnsureCapacity

Indexer

1 ' Fig. 15.11: StringBuilderFeatures.vb2 ' Demonstrating some features of class StringBuilder.3 4 Imports System.Text5 Imports System.Windows.Forms6 7 Module modBuilderFeatures8 9 Sub Main()10 Dim i As Integer11 Dim buffer As StringBuilder = _12 New StringBuilder("Hello, how are you?")13 14 ' use Length and Capacity properties15 Dim output As String = "buffer = " & buffer.ToString & _16 vbCrLf & "Length = " & buffer.Length & vbCrLf & _17 "Capacity = " & buffer.Capacity18 19 ' use EnsureCapacity method20 buffer.EnsureCapacity(75)21 22 output &= vbCrLf & vbCrLf & "New capacity = " & _23 buffer.Capacity24 25 ' truncate StringBuilder by setting Length property26 buffer.Length = 1027 28 output &= vbCrLf & vbCrLf & "New Length = " & _29 buffer.Length & vbCrLf & "buffer = "30 31 ' use StringBuilder Indexer32 For i = 0 To buffer.Length - 133 output &= buffer(i)34 Next35

Creates new StringBuilder with contents of String argument

Returns number of characters stored (19)

Returns capacity of StringBuilder (32)

Ensures that capacity is at least 75

New capacity is 76 (one larger than 75)

Reduces contents to the first ten characters.Does not alter capacity

Displays length of internal string (10)

Indexer used to return character at position i

Page 30: Chapter 15 – Strings, Characters and Regular Expressions

2002 Prentice Hall.All rights reserved.

Outline30

StringBuilderFeatures.vb

36 MessageBox.Show(output, "StringBuilder Features")37 End Sub ' Main38 39 End Module ' modBuilderFeatures

Page 31: Chapter 15 – Strings, Characters and Regular Expressions

2002 Prentice Hall.All rights reserved.

Outline31

StringBuilderAppend.vb

Calls to Append

1 ' Fig. 15.12: StringBuilderAppend.vb2 ' Demonstrating StringBuilder Append methods.3 4 Imports System.Text5 Imports System.Windows.Forms6 7 Module modBuilderAppend8 9 Sub Main()10 Dim objectValue As Object = "hello"11 Dim stringValue As String = "good bye"12 Dim characterArray() As Char = {"a"c, "b"c, "c"c, _13 "d"c, "e"c, "f"c}14 15 Dim booleanValue As Boolean = True16 Dim characterValue As Char = "Z"c17 Dim integerValue As Integer = 718 Dim longValue As Long = 100000019 Dim singleValue As Single = 2.5F20 Dim doubleValue As Double = 33.33321 Dim buffer As StringBuilder = New StringBuilder()22 23 ' use method Append to append values to buffer24 buffer.Append(objectValue)25 buffer.Append(" ")26 buffer.Append(stringValue)27 buffer.Append(" ")28 buffer.Append(characterArray)29 buffer.Append(" ")30 buffer.Append(characterArray, 0, 3)31 buffer.Append(" ")32 buffer.Append(booleanValue)33 buffer.Append(" ")34 buffer.Append(characterValue)35 buffer.Append(" ")

Create empty StringBuilder

19 different overloaded Append methods allow various data types (in this case an Object) to be concatenated onto StringBuilder contents

Appends a String literal

Appends a String object

Appends each value in an array of charactersAppends 3 characters from the array, starting at index 0

Appends string representation of Boolean value

Appends a single character

Page 32: Chapter 15 – Strings, Characters and Regular Expressions

2002 Prentice Hall.All rights reserved.

Outline32

StringBuilderAppend.vb

36 buffer.Append(integerValue)37 buffer.Append(" ")38 buffer.Append(longValue)39 buffer.Append(" ")40 buffer.Append(singleValue)41 buffer.Append(" ")42 buffer.Append(doubleValue)43 44 MessageBox.Show("buffer = " & buffer.ToString(), _45 "Demonstrating StringBuilder Append Methods", _46 MessageBoxButtons.OK, MessageBoxIcon.Information)47 End Sub ' Main48 49 End Module ' modBuilderAppend

Appends an integer

Appends a Long integer

Appends a Single

Appends a Double

Page 33: Chapter 15 – Strings, Characters and Regular Expressions

2002 Prentice Hall.All rights reserved.

Outline33

StringBuilderAppendFormat.vb

Call to AppendFormat

1 ' Fig. 15.13: StringBuilderAppendFormat.vb2 ' Demonstrating method AppendFormat.3 4 Imports System.Text5 Imports System.Windows.Forms6 7 Module modBuilderAppendFormat8 9 Sub Main()10 Dim buffer As StringBuilder = New StringBuilder()11 Dim string1, string2 As String12 13 ' formatted string14 string1 = "This {0} costs :{1:C}." & vbCrLf15 16 ' string1 argument array17 Dim objectArray As Object() = New Object(1) {}18 19 objectArray(0) = "car"20 objectArray(1) = 1234.5621 22 ' append to buffer formatted string with argument23 buffer.AppendFormat(string1, objectArray)24 25 ' formatted string26 string2 = "Number:{0:D3}. " & vbCrLf & _27 "Number right aligned with spaces:{0, 4}." & vbCrLf & _28 "Number left aligned with spaces:{0, -4}."29 30 ' append to buffer formatted string with argument31 buffer.AppendFormat(string2, 5)32

Creates a String with formatting information

Indicates value should be formatted as a currency

Format allows a value to be placed in a String at a particular location

Substitutes values contained in objectArray for formats in string1 and appends result to buffer contents

Value of second argument is substituted for formats in string2, then contents are appended to buffer

Formats value as a three-digit decimal integer

Value displayed in four spaces, aligned to the right

Negative number aligns value to the left

Page 34: Chapter 15 – Strings, Characters and Regular Expressions

2002 Prentice Hall.All rights reserved.

Outline34

StringBuilderAppendFormat.vb

33 ' display formatted strings34 MessageBox.Show(buffer.ToString(), "Using AppendFormat", _35 MessageBoxButtons.OK, MessageBoxIcon.Information)36 End Sub ' Main37 38 End Module ' modBuilderAppendFormat

Page 35: Chapter 15 – Strings, Characters and Regular Expressions

2002 Prentice Hall.All rights reserved.

Outline35

StringBuilderInsertRemove.vb

Calls to Insert

1 ' Fig. 15.14: StringBuilderInsertRemove.vb2 ' Demonstrating methods Insert and Remove of the 3 ' StringBuilder class.4 5 Imports System.Text6 Imports System.Windows.Forms7 8 Module modBuilderInsertRemove9 10 Sub Main()11 Dim objectValue As Object = "hello"12 Dim stringValue As String = "good bye"13 Dim characterArray() As Char = {"a"c, "b"c, "c"c, _14 "d"c, "e"c, "f"c}15 16 Dim booleanValue As Boolean = True17 Dim characterValue As Char = "K"c18 Dim integerValue As Integer = 719 Dim longValue As Long = 1000000020 Dim singleValue As Single = 2.521 Dim doubleValue As Double = 33.33322 Dim buffer As StringBuilder = New StringBuilder()23 Dim output As String24 25 ' insert values into buffer26 buffer.Insert(0, objectValue)27 buffer.Insert(0, " ")28 buffer.Insert(0, stringValue)29 buffer.Insert(0, " ")30 buffer.Insert(0, characterArray)31 buffer.Insert(0, " ")32 buffer.Insert(0, booleanValue)33 buffer.Insert(0, " ")34 buffer.Insert(0, characterValue)35 buffer.Insert(0, " ")

18 overloaded Insert methods allow values of various data types to be inserted at a specific location

Page 36: Chapter 15 – Strings, Characters and Regular Expressions

2002 Prentice Hall.All rights reserved.

Outline36

StringBuilderInsertRemove.vb

Calls to Remove

36 buffer.Insert(0, integerValue)37 buffer.Insert(0, " ")38 buffer.Insert(0, longValue)39 buffer.Insert(0, " ")40 buffer.Insert(0, singleValue)41 buffer.Insert(0, " ")42 buffer.Insert(0, doubleValue)43 buffer.Insert(0, " ")44 45 output = "buffer after inserts:" & vbCrLf & _46 buffer.ToString() & vbCrLf & vbCrLf47 48 buffer.Remove(12, 1) ' delete 5 in 2.549 buffer.Remove(2, 4) ' delete .333 in 33.33350 51 output &= "buffer after Removes:" & vbCrLf & _52 buffer.ToString()53 54 MessageBox.Show(output, "Demonstrating StringBuilder " & _55 "Insert and Remove Methods", MessageBoxButtons.OK, _56 MessageBoxIcon.Information)57 End Sub ' Main 58 59 End Module ' modBuilderInsertRemove

Method Remove deletes the number of characters specified by the second argument, starting at the location specified by the first argument

Page 37: Chapter 15 – Strings, Characters and Regular Expressions

2002 Prentice Hall.All rights reserved.

Outline37

StringBuilderReplace.vb

Calls to Replace

1 ' Fig. 15.15: StringBuilderReplace.vb2 ' Demonstrating method Replace.3 4 Imports System.Text5 Imports System.Windows.Forms6 7 Module modBuilderReplace8 9 Sub Main()10 Dim builder1 As StringBuilder = _11 New StringBuilder("Happy Birthday Jane")12 13 Dim builder2 As StringBuilder = _14 New StringBuilder("good bye greg")15 16 Dim output As String = "Before Replacements:" & vbCrLf & _17 builder1.ToString() & vbCrLf & builder2.ToString()18 19 builder1.Replace("Jane", "Greg")20 builder2.Replace("g"c, "G"c, 0, 5)21 22 output &= vbCrLf & vbCrLf & "After Replacements:" & _23 vbCrLf & builder1.ToString() & vbCrLf & _24 builder2.ToString()25 26 MessageBox.Show(output, _27 "Using StringBuilder method Replace", _28 MessageBoxButtons.OK, MessageBoxIcon.Information)29 End Sub ' Main30 31 End Module ' modBuilderReplace

Method Replace locates all instances of the first argument and replaces them with the contents of the second argument

In this case, first character is replaced by second character if it falls in the 5 positions starting at index 0

Page 38: Chapter 15 – Strings, Characters and Regular Expressions

2002 Prentice Hall.All rights reserved.

Outline38

StringBuilderReplace.vb

Page 39: Chapter 15 – Strings, Characters and Regular Expressions

2002 Prentice Hall. All rights reserved.

39

15.15  Char Methods

• Structures– Program building blocks similar to classes

– Encapsulate value types

– Have methods and properties

– Passed by value unless keyword ByRef used

– Many data type keywords (e.g., Integer) are aliases for structures (e.g., System.Int32)

– Created using keyword Structure– Char is example of structure

• Upcoming example: Char’s Shared character-testing methods and case-conversion methods

Page 40: Chapter 15 – Strings, Characters and Regular Expressions

2002 Prentice Hall.All rights reserved.

Outline40

CharMethods.vb

Define FrmCharacter

cmdAnalyze_Click

Call to BuildOutput

BuildOutput

Call to IsDigit

1 ' Fig. 15.16: CharMethods.vb2 ' Demonstrates Shared character testing methods 3 ' from Char structure4 5 Public Class FrmCharacter6 Inherits Form78 Friend WithEvents lblEnter As Label ' prompts for input910 Friend WithEvents txtInput As TextBox ' reads a Char11 Friend WithEvents txtOutput As TextBox ' displays results1213 ' reads and displays information about input14 Friend WithEvents cmdAnalyze As Button15 16 ' Visual Studio .NET generated code17 18 ' handle cmdAnalyze Click19 Private Sub cmdAnalyze_Click(ByVal sender As System.Object, _20 ByVal e As System.EventArgs) Handles cmdAnalyze.Click21 22 Dim character As Char = Convert.ToChar(txtInput.Text)23 24 BuildOutput(character)25 End Sub ' cmdAnalyze_Click26 27 ' display character information in txtOutput28 Public Sub BuildOutput(ByVal inputCharacter)29 Dim output As String30 31 output = "is digit: " & _32 Char.IsDigit(inputCharacter) & vbCrLf33

Reads user input

Generates program output

Method IsDigit returns a Boolean indicating if argument is a digit

Page 41: Chapter 15 – Strings, Characters and Regular Expressions

2002 Prentice Hall.All rights reserved.

Outline41

CharMethods.vb

Call to IsLetter

Call to IsLetterOrDigit

Call to IsLower

Call to IsUpper

Call to ToUpper

Call to ToLower

Call to IsPunctuation

Call to IsSymbol

34 output &= "is letter: " & _35 Char.IsLetter(inputCharacter) & vbCrLf36 37 output &= "is letter or digit: " & _38 Char.IsLetterOrDigit(inputCharacter) & vbCrLf 39 40 output &= "is lower case: " & _41 Char.IsLower(inputCharacter) & vbCrLf42 43 output &= "is upper case or digit: " & _44 Char.IsUpper(inputCharacter) & vbCrLf45 46 output &= "to upper case: " & _47 Char.ToUpper(inputCharacter) & vbCrLf48 49 output &= "to lower case: " & _50 Char.ToLower(inputCharacter) & vbCrLf51 52 output &= "is punctuation: " & _53 Char.IsPunctuation(inputCharacter) & vbCrLf54 55 output &= "is symbol: " & Char.IsSymbol(inputCharacter)56 57 txtOutput.Text = output58 End Sub ' BuildOutput59 60 End Class ' FrmCharacter

Determines if argument is a letter

Determines if argument is a letter or a digit

Determines if argument is lowercase

Determines if argument is uppercase

Converts argument to uppercase

Converts argument to lowercase

Determines if argument is a punctuation character

Determines if argument is a symbol

Page 42: Chapter 15 – Strings, Characters and Regular Expressions

2002 Prentice Hall.All rights reserved.

Outline42

CharMethods.vb

Page 43: Chapter 15 – Strings, Characters and Regular Expressions

2002 Prentice Hall.All rights reserved.

Outline43

Card.vb

Define CCard

Constructor

ToString

1 ' Fig. 15.17: Card.vb2 ' Stores suit and face information on each card.3 4 Class CCard5 Private face As String6 Private suit As String7 8 Public Sub New(ByVal faceValue As String, _9 ByVal suitValue As String)10 11 face = faceValue12 suit = suitValue13 End Sub ' New14 15 Public Overrides Function ToString() As String16 Return face & " of " & suit17 End Function ' ToString18 19 End Class ' CCard

Creates a CCard object with specified face and suit value

Method ToString overridden to provide a convenient and consistent String representation of the CCard object

Page 44: Chapter 15 – Strings, Characters and Regular Expressions

2002 Prentice Hall.All rights reserved.

Outline44

DeckOfCards.vb

Define FrmDeck

FrmDeck_Load

1 ' Fig. 15.18: DeckOfCards.vb2 ' Simulating card drawing and shuffling.3 4 Public Class FrmDeck5 Inherits Form67 Friend WithEvents lblDisplay As Label ' displays dealt card8 Friend WithEvents lblStatus As Label ' number of cards dealt 910 Friend WithEvents cmdDeal As Button ' deal one card11 Friend WithEvents cmdShuffle As Button ' shuffle cards1213 ' Visual Studio.NET generated code1415 Private currentCard As Integer16 Private randomObject As Random = New Random()17 Private deck As CCard() = New CCard(51) {}18 19 ' handles form at load time20 Public Sub FrmDeck_Load(ByVal sender As System.Object, _21 ByVal e As System.EventArgs) Handles MyBase.Load22 23 Dim faces As String() = {"Ace", "Deuce", "Three", _24 "Four", "Five", "Six", "Seven", "Eight", "Nine", _25 "Ten", "Jack", "Queen", "King"}26 27 Dim suits As String() = {"Hearts", "Diamonds", "Clubs", _28 "Spades"}29 30 Dim i As Integer31 32 ' no cards have been drawn33 currentCard = -134

An array of CCard objects

Ordered array of face names

Array of suit names

Initializes the deck array when form loads

Page 45: Chapter 15 – Strings, Characters and Regular Expressions

2002 Prentice Hall.All rights reserved.

Outline45

DeckOfCards.vb

cmdDeal_Click

Call to DealCard

Call to ToString

Shuffle

35 ' initialize deck36 For i = 0 To deck.GetUpperBound(0)37 deck(i) = New CCard(faces(i Mod 13), suits(i Mod 4))38 Next39 40 End Sub ' DeckCards41 42 ' handles cmdDeal Click43 Private Sub cmdDeal_Click(ByVal sender As System.Object, _44 ByVal e As System.EventArgs) Handles cmdDeal.Click45 46 Dim dealt As CCard = DealCard()47 48 ' if dealt card is Nothing, then no cards left49 ' player must shuffle cards50 If Not (dealt Is Nothing) Then51 lblDisplay.Text = dealt.ToString()52 lblStatus.Text = "Card #: " & currentCard53 Else54 lblDisplay.Text = "NO MORE CARDS TO DEAL"55 lblStatus.Text = "Shuffle cards to continue"56 End If57 58 End Sub ' cmdDeal_Click59 60 ' shuffle cards61 Public Sub Shuffle()62 Dim i As Integer63 Dim j As Integer64 Dim temporaryValue As CCard65 66 currentCard = -167

Create 52 unique CCard objects

Deals the next CCard object from the deck

Deals a single card or returns Nothing if deck is empty

Calling overridden method ToString produces a String representation of the CCard object

Reorders deck randomly

Page 46: Chapter 15 – Strings, Characters and Regular Expressions

2002 Prentice Hall.All rights reserved.

Outline46

DeckOfCards.vb

DealCard

68 ' swap each card with random card69 For i = 0 To deck.GetUpperBound(0)70 j = randomObject.Next(52)71 72 ' swap cards73 temporaryValue = deck(i) 74 deck(i) = deck(j) 75 deck(j) = temporaryValue 76 Next77 78 cmdDeal.Enabled = True79 End Sub ' Shuffle80 81 Public Function DealCard() As CCard82 83 ' if there is a card to deal then deal it 84 ' otherwise signal that cards need to be shuffled by 85 ' disabling cmdDeal and returning Nothing86 If (currentCard + 1) < deck.GetUpperBound(0) Then87 currentCard += 188 89 Return deck(currentCard)90 Else91 cmdDeal.Enabled = False92 93 Return Nothing94 End If95 96 End Function ' DealCard97

Each CCard in deck is swapped into another random position

Returns the next CCard from the deck

If no cards are left, disable cmdDeal and return Nothing

Page 47: Chapter 15 – Strings, Characters and Regular Expressions

2002 Prentice Hall.All rights reserved.

Outline47

DeckOfCards.vb

cmdShuffle_Click

Call to Shuffle

98 ' cmdShuffle_Click99 Private Sub cmdShuffle_Click(ByVal sender As System.Object, _100 ByVal e As System.EventArgs) Handles cmdShuffle.Click101 102 lblDisplay.Text = "SHUFFLING..."103 104 Shuffle()105 106 lblDisplay.Text = "DECK IS SHUFFLED"107 End Sub ' cmdShuffle_Click108 109 End Class ' FrmDeck

Calls Shuffle to place cards in random order

Page 48: Chapter 15 – Strings, Characters and Regular Expressions

2002 Prentice Hall.All rights reserved.

Outline48

DeckOfCards.vb

Page 49: Chapter 15 – Strings, Characters and Regular Expressions

2002 Prentice Hall. All rights reserved.

49

15.17  Regular Expressions and Class Regex

• Regular expressions– Specially formatted Strings used to find patterns in text

– Find special formats (e.g., ZIP codes, telephone numbers)

– Class Regex (System.Text.RegularExpressions)• Method Match returns object of class Match• Method Matches returns MatchCollection object

Page 50: Chapter 15 – Strings, Characters and Regular Expressions

2002 Prentice Hall. All rights reserved.

50

15.17  Regular Expressions and Class Regex

• Regular expression syntax– Character literals

• Match a specific character

• Usually indicated by the character itself

– Dot character, .• Matches any single character except newline

– Character classes• Match any of a designated set of characters

• Preceded by a backslash, \ (used as escape character)

– Distinguishes character class from character literal

Page 51: Chapter 15 – Strings, Characters and Regular Expressions

2002 Prentice Hall. All rights reserved.

51

15.17  Regular Expressions and Class Regex

Character Matches Character Matches \d any digit \D any non-digit

\w any word character \W any non-word character

\s any whitespace \S any non-whitespace

Fig. 15.19 Character classes.

Fig. 15.19 Character classes.

Page 52: Chapter 15 – Strings, Characters and Regular Expressions

2002 Prentice Hall. All rights reserved.

52

15.17  Regular Expressions and Class Regex

• Character sets– Several characters enclosed in square brackets, []– Matches any single listed character

– Ranges• Dash, -, placed between two characters

• Match any character lexicographically within range

– Can contain multiple ranges and individual characters

– If first character in set is ^, characters not in set are matched

Page 53: Chapter 15 – Strings, Characters and Regular Expressions

2002 Prentice Hall. All rights reserved.

53

15.17  Regular Expressions and Class Regex

• Basic quantifiers– * quantifier

• Matches zero or more instances of preceding character

– + quantifier• Matches one or more instances of preceding character

– "A+" versus "A*" • Both match strings "A", "AA", "AAA", etc.

• Only "A*" matches empty string

• Upcoming example: Regular expressions checking birthdays

Page 54: Chapter 15 – Strings, Characters and Regular Expressions

2002 Prentice Hall.All rights reserved.

Outline54

RegexMatches.vb

Regex constructor

Call to Matches

1 ' Fig. 15.20: RegexMatches.vb2 ' Demonstrating Class Regex.3 4 Imports System.Text.RegularExpressions5 Imports System.Windows.Forms6 7 Module modRegexMatches8 9 Sub Main()10 Dim output As String = ""11 Dim myMatch As Match12 13 ' create regular expression14 Dim expression As Regex = _ 15 New Regex("J.*\d[0-35-9]-\d\d-\d\d")16 17 Dim string1 As String = "Jane's Birthday is 05-12-75" & _18 vbCrLf & "Dave's Birthday is 11-04-68" & vbCrLf & _19 "John's Birthday is 04-28-73" & vbCrLf & _20 "Joe's Birthday is 12-17-77"21 22 ' match regular expression to string and 23 ' print out all matches24 For Each myMatch In expression.Matches(string1)25 output &= myMatch.ToString() & vbCrLf26 Next27 28 MessageBox.Show(output, "Using Class Regex", _29 MessageBoxButtons.OK, MessageBoxIcon.Information)30 End Sub ' Main31 32 End Module ' modRegexMatches

Creates new Regex object

Character literal J matches single J character

.* matches zero or more instances of any character except newline

\d matches any single digit Expression in brackets matches any single character in range 0-3 or 5-9.Does not match digit 4 or any non-digit

Overall expression matches strings starting with J, ending with a properly-formatted date not in April

- character outside brackets treated as character literal.Matches single - character

Call to Matches returns a MatchCollection, containing every Match found in argument String

Call to ToString returns text that matched regular expression

Regular expression String is passed to Regex constructor

Page 55: Chapter 15 – Strings, Characters and Regular Expressions

2002 Prentice Hall.All rights reserved.

Outline55

RegexMatches.vb

Page 56: Chapter 15 – Strings, Characters and Regular Expressions

2002 Prentice Hall. All rights reserved.

56

15.17  Regular Expressions and Class Regex

• Matching substrings versus entire strings– Normally, any matching substring may be returned– ^ character

• Matches beginning of string

– $ character• Matches end of string

– Regular expression enclosed between ^ and $ determines whether entire string matches

• | character– Used to match expression to the left or to the right

• Parentheses, ()– Allow sub-expressions to be grouped

Page 57: Chapter 15 – Strings, Characters and Regular Expressions

2002 Prentice Hall. All rights reserved.

57

15.17  Regular Expressions and Class Regex

• Quantifiers– Applied to sub-expressions enclosed in parentheses

• Match sub-expression multiple times

– Greedy behavior• Match as many occurrences as possible for successful match

• By default, all quantifiers are greedy

– Lazy behavior• Invoked by following any quantifier with ? character

• Match as few occurrences as possible for successful match

• Upcoming example: Validating user information using regular expressions

Page 58: Chapter 15 – Strings, Characters and Regular Expressions

2002 Prentice Hall. All rights reserved.

58

15.17  Regular Expressions and Class Regex

Quantifier Matches * Matches any number of occurrences of pattern including zero.

+ Matches one or more occurrences of the pattern.

? Matches zero or one occurrences of the pattern.

{n} Matches exactly n occurrences.

{n,m} Matches between n and m (inclusively) occurrences.

Fig. 15.21 Quantifiers used regular expressions.

Fig. 15.21 Quantifiers used regular expressions.

Page 59: Chapter 15 – Strings, Characters and Regular Expressions

2002 Prentice Hall.All rights reserved.

Outline59

Validate.vb

Define FrmValid

1 ' Fig. 15.22: Validate.vb2 ' Validate user information using regular expressions.3 4 Imports System.Text.RegularExpressions5 6 Public Class FrmValid7 Inherits Form89 ' field labels10 Friend WithEvents lblLast As Label11 Friend WithEvents lblFirst As Label 12 Friend WithEvents lblAddress As Label 13 Friend WithEvents lblCity As Label14 Friend WithEvents lblState As Label15 Friend WithEvents lblZip As Label16 Friend WithEvents lblPhone As Label1718 ' field inputs19 Friend WithEvents txtLast As TextBox20 Friend WithEvents txtFirst As TextBox21 Friend WithEvents txtAddress As TextBox22 Friend WithEvents txtCity As TextBox23 Friend WithEvents txtState As TextBox24 Friend WithEvents txtZip As TextBox25 Friend WithEvents txtPhone As TextBox2627 Friend WithEvents cmdOK As Button ' validate all fields 28 29 ' Visual Studio .NET generated code 30

Page 60: Chapter 15 – Strings, Characters and Regular Expressions

2002 Prentice Hall.All rights reserved.

Outline60

Validate.vb

cmdOK_Click

Call to Match

31 ' handles cmdOK Click event32 Private Sub cmdOK_Click(ByVal sender As System.Object, _33 ByVal e As System.EventArgs) Handles cmdOK.Click34 35 ' ensures no textboxes are empty36 If (txtPhone.Text = "" OrElse txtZip.Text = "" OrElse _37 txtState.Text = "" OrElse txtCity.Text = "" OrElse _38 txtAddress.Text = "" OrElse txtFirst.Text = "" OrElse _39 txtLast.Text = "") Then40 41 ' display popup box 42 MessageBox.Show("Please fill in all fields", "Error", _43 MessageBoxButtons.OK, MessageBoxIcon.Error)44 45 ' set focus to txtLast46 txtLast.Focus()47 48 Return49 End If5051 ' if last name format invalid show message52 If Not Regex.Match(txtLast.Text, _53 "^[A-Z][a-zA-Z]*$").Success Then54 55 ' last name was incorrect56 MessageBox.Show("Invalid Last Name", "Message")57 txtLast.Focus()58 59 Return60 End If61

Matches beginning of String

Matches one uppercase letter

Matches any letter

* quantifier is used to match zero or more instances from the preceding range, [a-zA-Z]

$ matches end of String

Whole regular expression matches capitalized names with exactly one word

Page 61: Chapter 15 – Strings, Characters and Regular Expressions

2002 Prentice Hall.All rights reserved.

Outline61

Validate.vb

Calls to Match

62 ' if first name format invalid show message63 If Not Regex.Match(txtFirst.Text, _64 "^[A-Z][a-zA-Z]*$").Success Then65 66 ' first name was incorrect67 MessageBox.Show("Invalid First Name", "Message")68 txtFirst.Focus()69 70 Return71 End If72 73 ' if address format invalid show message74 If Not Regex.Match(txtAddress.Text, "^[0-9]+\s+([a-zA-Z]" & _75 "+|[a-zA-Z]+\s[a-zA-Z]+)$").Success Then76 77 ' address was incorrect78 MessageBox.Show("Invalid Address", "Message")79 txtAddress.Focus()80 81 Return82 End If83 84 ' if city format invalid show message85 If Not Regex.Match(txtCity.Text, "^([a-zA-Z]+|[a-zA-Z]" & _86 "+\s[a-zA-Z]+)$").Success Then87 88 ' city was incorrect89 MessageBox.Show("Invalid City", "Message")90 txtCity.Focus()91 92 Return93 End If94

Same as previous regular expression^ and $ are used to force matching of entire String

+ quantifier used to match one or more digits

\s+ matches one or more whitespace characters

Parentheses used to group sub-expressions

| character is used to match either of two sub-expressions

Expression to the right of | character matches two words

Expression to left of | character matches one word

Expression matches one or two words

Expression matches a number, followed by whitespace and one or more words

Page 62: Chapter 15 – Strings, Characters and Regular Expressions

2002 Prentice Hall.All rights reserved.

Outline62

Validate.vb

Calls to Match

95 ' if state format invalid show message96 If Not Regex.Match(txtState.Text, _97 "^([a-zA-Z]+|[a-zA-Z]+\s[a-zA-Z]+)$").Success Then98 99 ' state was incorrect100 MessageBox.Show("Invalid State", "Message")101 txtState.Focus()102 103 Return104 End If105 106 ' if zip code format invalid show message107 If Not Regex.Match(txtZip.Text, "^\d{5}$").Success Then108 109 ' zip code was incorrect110 MessageBox.Show("Invalid zip code", "Message")111 txtZip.Focus()112 113 Return114 End If115 116 ' if phone number format invalid show message117 If Not Regex.Match(txtPhone.Text, "^[1-9]" & _118 "\d{2}-[1-9]\d{2}-\d{4}$").Success Then119 120 ' phone was incorrect121 MessageBox.Show("Invalid Phone Number", "Message")122 txtPhone.Focus()123 124 Return125 End If126

Expression matches one or two words

\d{5} matches any five digits.^ and $ prevent numbers with extra digits from containing a match

Matches a non-zero digitMatches any two digits

Matches a non-zero digit, followed by two digits

Phone number must include - characters

Matches last four digits

Page 63: Chapter 15 – Strings, Characters and Regular Expressions

2002 Prentice Hall.All rights reserved.

Outline63

Validate.vb

127 ' information is valid, signal user and exit application128 Me.Hide()129 MessageBox.Show("Thank you!", "Information Correct", _130 MessageBoxButtons.OK, MessageBoxIcon.Information)131 132 Application.Exit()133 End Sub ' cmdOK_Click134 135 End Class ' FrmValid

Page 64: Chapter 15 – Strings, Characters and Regular Expressions

2002 Prentice Hall.All rights reserved.

Outline64

Validate.vb

Page 65: Chapter 15 – Strings, Characters and Regular Expressions

2002 Prentice Hall. All rights reserved.

65

15.17  Regular Expressions and Class Regex

• Regex method Replace– Replaces text in a String wherever a regular expression is

matched

• Regex method Split– Divides a String into separate Strings wherever regular

expression is matched

– Returns array of Strings

– Upcoming example: Regex methods Replace and Split

Page 66: Chapter 15 – Strings, Characters and Regular Expressions

2002 Prentice Hall.All rights reserved.

Outline66

RegexSubstitution.vb

Call to Replace

Call to Replace

Call to Replace

1 ' Fig. 15.23: RegexSubstitution.vb2 ' Using Regex method Replace.3 4 Imports System.Text.RegularExpressions5 Imports System.Windows.Forms6 7 Module modRegexSubstitution8 9 Sub Main()10 Dim testString1 As String = _11 "This sentence ends in 5 stars *****"12 13 Dim testString2 As String = "1, 2, 3, 4, 5, 6, 7, 8"14 Dim testRegex1 As Regex = New Regex("stars")15 Dim testRegex2 As Regex = New Regex("\d")16 Dim results As String()17 Dim resultString As String18 Dim output As String = "Original String 1" & vbTab & _19 vbTab & vbTab & testString12021 testString1 = Regex.Replace(testString1, "\*", "^")22 23 output &= vbCrLf & "^ substituted for *" & vbTab & _24 vbTab & vbTab & testString125 26 testString1 = testRegex1.Replace(testString1, "carets")2728 output &= vbCrLf """carets"" substituted for " & _29 """stars""" & vbTab & testString130 31 output &= vbCrLf & "Every word replaced by " & _32 """word""" & vbTab & _33 Regex.Replace(testString1, "\w+", "word")34

Call to Replace substitutes a ^ character for each * character

Literal character * must be preceded by \ character to differentiate from * quantifier

Replaces "stars" with "carets" in testString1

Replaces every sequence of one or more word characters with "word".

Page 67: Chapter 15 – Strings, Characters and Regular Expressions

2002 Prentice Hall.All rights reserved.

Outline67

RegexSubstitution.vb

Call to Replace

Call to Split

35 output &= vbCrLf & vbCrLf & "Original String 2" & _36 vbTab & vbTab & vbTab & testString237 38 output &= vbCrLf & "First 3 digits replaced by " & _39 """digit""" & vbTab & _40 testRegex2.Replace(testString2, "digit", 3) 4142 output &= vbCrLf & "String split at commas" & vbTab & _43 vbTab & "["44 45 results = Regex.Split(testString2, ",\s*")46 47 For Each resultString In results48 output &= """" & resultString & """, "49 Next50 51 output = output.Substring(0, output.Length - 2) & "]"52 53 MessageBox.Show(output, _54 "Substitution Using Regular Expressions")55 End Sub ' Main56 57 End Module ' modRegexSubstitution

Third argument indicates how many replacements to make.Here, only the first three digits are replaced

Call to Split separates testString2 into the substrings between matches for the regular expression