Chapter 15 – Strings, Characters and Regular Expressions Outline

advertisement
Chapter 15 – Strings, Characters and
Regular Expressions
Outline
15.1
15.2
15.3
15.4
15.5
15.6
15.7
15.8
15.9
15.10
15.11
15.12
15.13
15.14
15.15
15.16
15.17
Introduction
Fundamentals of Characters and Strings
String Constructors
String Length and Chars Properties and CopyTo Method
Comparing Strings
String Method GetHashCode
Locating Characters and Substrings in Strings
Extracting Substrings from Strings
Concatenating Strings
Miscellaneous String Methods
Class StringBuilder
StringBuilder Indexer, Length and Capacity Properties,
and EnsureCapacity Method
StringBuilder Append and AppendFormat Methods
StringBuilder Insert, Remove and Replace Methods
Char Methods
Card Shuffling and Dealing Simulation
Regular Expressions and Class Regex
 2002 Prentice Hall. All rights reserved.
1
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
 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
 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
 2002 Prentice Hall. All rights reserved.
5
Outline
1
' Fig. 15.1: StringConstructor.vb
2
' Demonstrating String class constructors.
3
4
Imports System.Windows.Forms
StringConstructo
5
r.vb
6
Module modStringConstructor
ChrW
converts
Unicode
value
7
8
Sub Main()
34 to double quote character, "
9
Dim characterArray As Char()
Creates an array of type Char.
10
Dim output As String
Suffix
c required
when
using= ChrW(34)
11
Dim
quotes As
String
ChrW function
12
Dim originalString,
string1, string2, string3, _
Option
Strict
13
string4 As String
14
Creates
a literal
15
characterArray = New Char()
{"b"c,
"i"c, "r"c, _
String
object
16
"t"c, "h"c, " "c, "d"c, "a"c, "y"c}
Creates a new String object containing a
17
copy of characters in characterArray
18
' string initialization
19
originalString = "Welcome to VB.NET Programming!"
string1 and originalString
20
string1 = originalString
reference same literal String
object
Calls
to String
21
string2 = New String(characterArray)
22
string3 = New String(characterArray, 6, 3)
constructors
23
string4 = New String("C"c, 5)
24
Copies&characters
characterArray,
25
output = "string1 = " & quotes & string1
quotes & from
_
Creates
a
String
with
length
indicated
26
vbCrLf & "string2 = " & quotes & starting
string2at&the
quotes
_
index &indicated
by second
27 by the secondvbCrLf
& "string3
= "copies
& quotes & string3 & quotes & _
argument,
filled with
argument and continuing for the number of
28 of the character
vbCrLf
& "string4
passed
as the first= " & quotes & string4 & quotes
29
characters indicated by the third argument
argument
Each
instance
of
variable
30
MessageBox.Show(output, "String Class Constructors", _
quotes
represents a
31
MessageBoxButtons.OK,
MessageBoxIcon.Information)
32
End double
Sub ' Main
quote character, "
33
34
End Module ' modStringConstructor
 2002 Prentice Hall.
All rights reserved.
6
Outline
StringConstructo
r.vb
 2002 Prentice Hall.
All rights reserved.
7
Outline
1
' Fig. 15.2: StringMiscellaneous.vb
2
' Using properties Length and Chars
3
' of class string.
4
StringMiscellane
5
Imports System.Windows.Forms
ous.vb
6
7
Module modMiscellaneous
8
9
Sub Main()
10
Dim string1, output As String
11
Dim characterArray As Char()
12
Dim i As Integer
13
Dim quotes As String = ChrW(34)
14
15
string1 = "hello there"
16
characterArray = New Char(5) {}
17
18
' output string
Length
property& returns
19
output = "string1: " & quotes
& string1
quotesnumber of characters in string
20
21
' test Length property
22
output &= vbCrLf & "Length of string1: " & string1.Length
23
from a string
into an array.
24Copies characters
' loop through
characters
in string1 and display
25Respectively,
reversed
arguments
the location
Length'property
usedrepresent:
to loop backwards
through characters in string1
26at which tooutput
&=
vbCrLf
&
"The
string
reversed is: "
begin copying, the destination array,
27
to place the first- character
28the index into
For which
i = string1.Length
1 To 0 Step
-1
Returns
the character at
29copied and theoutput
&=
string1.Chars(i)
number of characters to copy
the position indicated by
30
Next
the integer argument
31
32
' copy characters from string1 into characterArray
33
string1.CopyTo(0, characterArray, 0, 5)
34
output &= vbCrLf & "The character array is: "
 2002 Prentice Hall.
35
All rights reserved.
8
36
For i = 0 To characterArray.Length - 1
37
output &= characterArray(i)
38
Next
39
Displays contents
of characterArray
40
MessageBox.Show(output,
"Demonstrating String" & _
41
" properties Length and Chars", _
42
MessageBoxButtons.OK, MessageBoxIcon.Information)
43
End Sub ' Main
44
45
End Module ' modMiscellaneous
Outline
StringMiscellane
ous.vb
 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
 2002 Prentice Hall. All rights reserved.
10
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
Outline
' Fig. 15.3: StringCompare.vb
' Comparing strings.
Imports System.Windows.Forms
StringCompare.vb
Module modCompare
Sub Main()
Dim string1 As String = "hello"
Dim string2 As String = "good bye"
Dim string3 As String = "Happy Birthday"
Dim string4 As String = "happy birthday"
Dim output As String
Dim quotes As String = ChrW(34)
' output values of four Strings
output = "string1 = " & quotes & string1 & quotes & _
vbCrLf & "string2 = " & quotes & string2 & quotes & _
vbCrLf
& "string3
= case" & quotes & string3 & quotes & _
Method
Equals
performs
vbCrLf & "string4 = " & quotes & string4 & quotes & _
sensitive
lexicographical
comparison
vbCrLf
& vbCrLf
' test for equality using Equals method
If (string1.Equals("hello")) Then
output &= "string1 equals " & quotes & "hello" & _
quotes & vbCrLf
Call to Equals
Else
Equalityoutput
operator
&= produces
"string1same
does not equal
"hello"Equals
& quotes & vbCrLf
results as method
" & quotes & _
End If
' test for equality with =
If string1 = "hello" Then
output &= "string1 equals " & quotes & "hello" & _
= operator
 2002 Prentice Hall.
All rights reserved.
11
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
quotes & vbCrLf
Else
Shared
method
Equals does
compares
output
&= "string1
not equal " & quotes & _
"hello"
&
quotes
&
vbCrLf
two Strings lexicographically
End If
' test for equality comparing case
Method CompareTo performs a
If (String.Equals(string3, string4)) Then
comparison.
output &= "string3 lexicographical
equals string4"
& vbCrLf
Else
Returns 0 if Strings are equal.
output &= "string3 does not equal string4" & vbCrLf
Returns –1 if argument String is greater.
End If
Outline
StringCompare.vb
Call to Equals
Returns 1 if calling String is greater
' test CompareTo
output &= vbCrLf & "string1.CompareTo(string2) is " & _
string1.CompareTo(string2) & vbCrLf & _
"string2.CompareTo(string1) is " & _
string2.CompareTo(string1) & vbCrLf & _
"string1.CompareTo(string1) is " & _
string1.CompareTo(string1) & vbCrLf & _
"string3.CompareTo(string4) is " & _
string3.CompareTo(string4) & vbCrLf & _
"string4.CompareTo(string3) is " & _
string4.CompareTo(string3) & vbCrLf & vbCrLf
Call to CompareTo
MessageBox.Show(output, "Demonstrating string" & _
" comparisons", MessageBoxButtons.OK, _
MessageBoxIcon.Information)
End Sub ' Main
End Module ' modCompare
 2002 Prentice Hall.
All rights reserved.
12
Outline
StringCompare.vb
 2002 Prentice Hall.
All rights reserved.
13
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
Outline
' Fig. 15.4: StringStartEnd.vb
' Demonstrating StartsWith and EndsWith methods.
Imports System.Windows.Forms
StringStartEnd.v
b
Module modStartEnd
Sub Main()
Dim strings As String()
Dim output As String = ""
Dim i As Integer
Dim quotes As String = ChrW(34)
strings = New String() {"started", "starting", _
Method StartsWith
determines whether
"ended",
"ending"}
the beginning of a String matches the
' test
everypassed
string
if it starts with "st"
String
as to
an see
argument
For i = 0 To strings.GetUpperBound(0)
If strings(i).StartsWith("st") Then
output &= quotes & strings(i) & quotes & _
" starts with " & quotes & "st" & quotes & vbCrLf
End If
Call to StartsWith
Next
Method
EndsWith
output
&= vbCrLf
determines
whether the end of a String matches
' test every string to see if it ends with
the String passed as an argument
For i = 0 To strings.GetUpperBound(0)
"ed"
If strings(i).EndsWith("ed") Then
output &= quotes & strings(i) & quotes & _
" ends with " & quotes & "ed" & quotes & vbCrLf
Call to EndsWith
 2002 Prentice Hall.
All rights reserved.
14
35
36
37
38
39
40
41
42
43
44
End If
Outline
Next
MessageBox.Show(output, "Demonstrating StartsWith and" & _
" EndsWith methods", MessageBoxButtons.OK, _
MessageBoxIcon.Information)
End Sub ' Main
StringStartEnd.v
b
End Module ' modStartEnd
 2002 Prentice Hall.
All rights reserved.
15
Outline
1
' Fig. 15.6: StringIndexMethods.vb
2
' Using String searching methods.
3
4
Imports System.Windows.Forms
StringIndexMetho
5
ds.vb
6
Module modIndexMethods
Alternative
to
ChrW(34).
7
8
Sub Main()
Two
consecutive double quotation
9
Dim
letters
As String
= "abcdefghijklmabcdefghijklm"
marks ("")
produces
one double
10
Dim output As String
quotation
mark in the String
11
Dim searchLetters
As Char() = New Char() {"c"c, "a"c, "$"c}
12
13
' test IndexOf to locate a character in a string
Calls to IndexOf
14
output &= """c"" is located at index " & _
15
letters.IndexOf("c"c)
Finds first occurrence of c in String letters
16
17
output &= vbCrLf & """a"" is located at index " & _
18
letters.IndexOf("a"c, 1)
Finds first occurrence of a in
19
letters,
starting
20
output &= vbCrLf & """$"" is located
at index
" & at
_ position 1
21
letters.IndexOf("$"c, 3, 5)
Searches for occurrence of $ in letters starting
22
at position
3 and searching for 5 characters.
23
' test LastIndexOf to find a character
in a string
Returns
–1, indicating
is no occurrence
24
output &= vbCrLf & vbCrLf & "Last ""c""
is located
at " &there
_
Calls to
25
"index " & letters.LastIndexOf("c"c)
Finds
last
occurrence
of
c
LastIndexOf
26Finds last occurrence of $ searching back
27from position
output
&= 5
vbCrLf
& "Last ""a"" is located at index " & _
15 for
characters.
28
Finds last occurrence of a by
Returns -1 letters.LastIndexOf("a"c, 25)
29
searching
back "from
30
output &= vbCrLf & "Last ""$"" is located
at index
& _ position 25
31
letters.LastIndexOf("$"c, 15, 5)
32
 2002 Prentice Hall.
All rights reserved.
16
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
Outline
' test IndexOf to locate a substring in a string
output &= vbCrLf & vbCrLf & """def"" is located at" & _
" index " & letters.IndexOf("def")
Whereas previous calls to
StringIndexMetho
output &= vbCrLf & """def"" is located at index
" & _ searched for
IndexOf
a
ds.vb
letters.IndexOf("def", 7)
character, this call finds
the substring
output &= vbCrLf & """hello"" is located at index
" & _ "def"
Calls to IndexOf
Searches for "def"5, 15)
letters.IndexOf("hello",
Searches for "hello" starting at
starting at position 7
5, continuing for 15 characters
' test LastIndexOf to find a substring position
in a string
output &= vbCrLf & vbCrLf & "Last ""def"" is located " & _
"at index " & letters.LastIndexOf("def")
Searches from
output &= vbCrLf & "Last ""def"" is
letters.LastIndexOf("def", 25)
Calls to
end
of String to
LastIndexOf
located at " find
& _ last occurrence of "def"
Searches back from position 25
output &= vbCrLf & "Last ""hello"" is located at " & _
Searches
"index " & letters.LastIndexOf("hello", 20, 15)
back from position
20 for 15 characters
' test IndexOfAny to find first occurrence of character
' in array
output &= vbCrLf & vbCrLf & "First occurrence of ""c""," & _
" ""a"" or ""$"" is located at " & _
Searches for first occurrence
Callsoftoany
IndexOfAny
letters.IndexOfAny(searchLetters)
character in searchLetters array
output &= vbCrLf & "First occurrence of ""c"", ""a"" or " & _
"""$"" is located at " & _
Searches for first occurrence
letters.IndexOfAny(searchLetters, 7)
of a character
in searchLetters, starting at position 7
output &= vbCrLf & "First occurrence of ""c"", ""a"" or " & _
"""$"" is located at " & _
Searches for 5
letters.IndexOfAny(searchLetters, 20, 5)
characters
starting at position 20
 2002 Prentice Hall.
All rights reserved.
17
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
Outline
' test LastIndexOfAny to find first occurrence of character
' in array
output &= vbCrLf & vbCrLf & "Last occurrence of ""c""," & _
" ""a"" or ""$"" is located at " & _
Searches for last
letters.LastIndexOfAny(searchLetters)
occurrence
of
StringIndexMetho
any in an array of characters
ds.vb
output &= vbCrLf & "Last occurrence of ""c"", ""a"" or " & _
"""$"" is located at " & _
letters.LastIndexOfAny(searchLetters, 1)
Searches back
Calls
to
from
position
1
LastIndexOfAny
output &= vbCrLf & "Last occurrence of ""c"", ""a"" or " & _
"""$"" is located at " & _
Searches
letters.LastIndexOfAny(searchLetters, 25, 5)
back from position
25 for 5 characters
MessageBox.Show(output, _
"Demonstrating String class index methods")
End Sub ' Main
End Module ' modIndexMethods
 2002 Prentice Hall.
All rights reserved.
18
Outline
StringIndexMetho
ds.vb
 2002 Prentice Hall.
All rights reserved.
19
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
' Fig. 15.7: SubString.vb
' Demonstrating the String Substring method.
Imports System.Windows.Forms
Module modSubString
Outline
Method Substring returns a new
String object generated by copying
SubString.vb
characters from the calling String.
One argument version returns characters
between position indicated and end of
"abcdefghijklmabcdefghijklm"
String
Sub Main()
Dim letters As String =
Dim output As String
Dim quotes As String = ChrW(34)
' invoke SubString method and pass it one parameter
output = "Substring from index 20 to end is " & _
quotes & letters.Substring(20) & quotes & vbCrLf
Call to Substring
' invoke SubString method and pass it two parameters
output &= "Substring from index 0 to 6 is " & _
quotes & letters.Substring(0, 6) & quotes
MessageBox.Show(output, _
"Demonstrating String method
End Sub ' Main
Call to Substring
Two argument version returns substring
Substring")
starting at position indicated by first argument
with length indicated by second argument
End Module ' modSubString
 2002 Prentice Hall.
All rights reserved.
20
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
12Shared method Concat returns a new
13String object
output
= "string1
= """ & string1 & """" & _
containing
the combined
14characters from
vbCrLf
&
"string2
= """ & string2 & """"
both original Strings
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 ' Main
23
24
End Module ' modSubConcat
Outline
SubConcatination
.vb
Call to Concat
 2002 Prentice Hall.
All rights reserved.
21
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
Outline
' Fig. 15.9: StringMiscellaneous.vb
' Demonstrating String methods Replace, ToLower, ToUpper, Trim,
' and ToString.
StringMiscellane
ous.vb
Imports System.Windows.Forms
Module modStringMiscellaneous
Sub Main()
Dim string1 As String = "cheers!"
Dim string2 As String = "GOOD BYE "
Dim string3 As String = "
spaces
Dim output As String
Dim quotes As String = ChrW(34)
Dim i As Integer
"
output = "string1 = " & quotes & string1 & quotes & _
vbCrLf & "string2 = " & quotes & string2 & quotes & _
vbCrLf & "string3 = " & quotes & string3 & quotes
Method Replace replaces every instance of the character
second argument
' call method Replace
by "the& first
argument
output &= vbCrLf & vbCrLf & indicated
"Replacing
quotes
& "e" with
& _ the
quotes & " with " & quotes & "E" & quotes & _
" in string1:
" & quotes
& string1.Replace("e"c,
"E"c)all& _
Method
ToUpper
creates a new String with
quotes
lowercase characters converted to uppercase
' call ToLower and ToUpper
Converts all uppercase
output &= vbCrLf & vbCrLf & "string1.ToUpper() = " & _
quotes & string1.ToUpper() & quotes & vbCrLf & _
"string2.ToLower() = " & quotes & string2.ToLower() & _
quotes
Call to Replace
characters to lowercase
Call to ToUpper
Call to ToLower
 2002 Prentice Hall.
All rights reserved.
22
33
34
35
36
37
38
39
40
41
42
43
44
45
' call Trim method
output &= vbCrLf & vbCrLf & "string3 after trim = " & _
quotes & string3.Trim() & quotes
Outline
Method Trim returns a copy of the StringMiscellane
' call ToString method
calling= String
with leading and ous.vb
output &= vbCrLf & vbCrLf & "string1
" & _
quotes & string1.ToString() & trailing
quoteswhitespace characters removed
MessageBox.Show(output, _
Method ToString
is provided
"Demonstrating
Miscellaneous
Stringfor
Methods")
class
String
because
String
is
End Sub ' Main
derived from class Object
Call to Trim
Call to ToString
End Module ' modStringMiscellaneous
 2002 Prentice Hall.
All rights reserved.
Download