 2002 Prentice Hall. All rights reserved. 1 Chapter 15 – Strings, Characters and Regular Expressions Outline 15.1Introduction 15.2 Fundamentals of Characters.

Slides:



Advertisements
Similar presentations
EcoTherm Plus WGB-K 20 E 4,5 – 20 kW.
Advertisements

Números.
1 A B C
Trend for Precision Soil Testing % Zone or Grid Samples Tested compared to Total Samples.
AGVISE Laboratories %Zone or Grid Samples – Northwood laboratory
Trend for Precision Soil Testing % Zone or Grid Samples Tested compared to Total Samples.
PDAs Accept Context-Free Languages
ALAK ROY. Assistant Professor Dept. of CSE NIT Agartala
AP STUDY SESSION 2.
1
EuroCondens SGB E.
Worksheets.
Slide 1Fig 26-CO, p.795. Slide 2Fig 26-1, p.796 Slide 3Fig 26-2, p.797.
Slide 1Fig 25-CO, p.762. Slide 2Fig 25-1, p.765 Slide 3Fig 25-2, p.765.
& dding ubtracting ractions.
Sequential Logic Design
Addition and Subtraction Equations
David Burdett May 11, 2004 Package Binding for WS CDL.
Create an Application Title 1Y - Youth Chapter 5.
Add Governors Discretionary (1G) Grants Chapter 6.
CALENDAR.
CHAPTER 18 The Ankle and Lower Leg
The 5S numbers game..
突破信息检索壁垒 -SciFinder Scholar 介绍
A Fractional Order (Proportional and Derivative) Motion Controller Design for A Class of Second-order Systems Center for Self-Organizing Intelligent.
Numerical Analysis 1 EE, NCKU Tien-Hao Chang (Darby Chang)
Media-Monitoring Final Report April - May 2010 News.
Break Time Remaining 10:00.
The basics for simulations
PP Test Review Sections 6-1 to 6-6
2000 Deitel & Associates, Inc. All rights reserved. Chapter 16 – Bits, Characters, Strings, and Structures Outline 16.1Introduction 16.2Structure Definitions.
2013 Fox Park Adopt-A-Hydrant Fund Raising & Beautification Campaign Now is your chance to take part in an effort to beautify our neighborhood by painting.
Regression with Panel Data
Dynamic Access Control the file server, reimagined Presented by Mark on twitter 1 contents copyright 2013 Mark Minasi.
Copyright © 2012, Elsevier Inc. All rights Reserved. 1 Chapter 7 Modeling Structure with Blocks.
Progressive Aerobic Cardiovascular Endurance Run
Biology 2 Plant Kingdom Identification Test Review.
Adding Up In Chunks.
MaK_Full ahead loaded 1 Alarm Page Directory (F11)
Facebook Pages 101: Your Organization’s Foothold on the Social Web A Volunteer Leader Webinar Sponsored by CACO December 1, 2010 Andrew Gossen, Senior.
TCCI Barometer September “Establishing a reliable tool for monitoring the financial, business and social activity in the Prefecture of Thessaloniki”
When you see… Find the zeros You think….
2011 WINNISQUAM COMMUNITY SURVEY YOUTH RISK BEHAVIOR GRADES 9-12 STUDENTS=1021.
Before Between After.
2011 FRANKLIN COMMUNITY SURVEY YOUTH RISK BEHAVIOR GRADES 9-12 STUDENTS=332.
Slide R - 1 Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Prentice Hall Active Learning Lecture Slides For use with Classroom Response.
12 October, 2014 St Joseph's College ADVANCED HIGHER REVISION 1 ADVANCED HIGHER MATHS REVISION AND FORMULAE UNIT 2.
Subtraction: Adding UP
1 Non Deterministic Automata. 2 Alphabet = Nondeterministic Finite Accepter (NFA)
1 hi at no doifpi me be go we of at be do go hi if me no of pi we Inorder Traversal Inorder traversal. n Visit the left subtree. n Visit the node. n Visit.
Static Equilibrium; Elasticity and Fracture
Converting a Fraction to %
Resistência dos Materiais, 5ª ed.
Clock will move after 1 minute
 2002 Prentice Hall. All rights reserved. 1 Chapter 15 – Strings, Chars Outline 15.1 Introduction 15.2 Fundamentals of Characters and String s 15.3 String.
& dding ubtracting ractions.
Lial/Hungerford/Holcomb/Mullins: Mathematics with Applications 11e Finite Mathematics with Applications 11e Copyright ©2015 Pearson Education, Inc. All.
Physics for Scientists & Engineers, 3rd Edition
Select a time to count down from the clock above
Copyright Tim Morris/St Stephen's School
1.step PMIT start + initial project data input Concept Concept.
WARNING This CD is protected by Copyright Laws. FOR HOME USE ONLY. Unauthorised copying, adaptation, rental, lending, distribution, extraction, charging.
A Data Warehouse Mining Tool Stephen Turner Chris Frala
1 Dr. Scott Schaefer Least Squares Curves, Rational Representations, Splines and Continuity.
1 Non Deterministic Automata. 2 Alphabet = Nondeterministic Finite Accepter (NFA)
Introduction Embedded Universal Tools and Online Features 2.
Schutzvermerk nach DIN 34 beachten 05/04/15 Seite 1 Training EPAM and CANopen Basic Solution: Password * * Level 1 Level 2 * Level 3 Password2 IP-Adr.
Presentation transcript:

 2002 Prentice Hall. All rights reserved. 1 Chapter 15 – Strings, Characters and Regular Expressions Outline 15.1Introduction 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 String s 15.8 Extracting Substrings from String s 15.9 Concatenating String s 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 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 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 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. Outline 5 StringConstructo r.vb ChrW function Calls to String constructors 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 As Char() 10 Dim output As String 11 Dim quotes As String = ChrW(34) 12 Dim originalString, string1, string2, string3, _ 13 string4 As String characterArray = New Char() {"b"c, "i"c, "r"c, _ 16 "t"c, "h"c, " "c, "d"c, "a"c, "y"c} ' string initialization 19 originalString = "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) output = "string1 = " & quotes & string1 & quotes & _ 26 vbCrLf & "string2 = " & quotes & string2 & quotes & _ 27 vbCrLf & "string3 = " & quotes & string3 & quotes & _ 28 vbCrLf & "string4 = " & quotes & string4 & quotes MessageBox.Show(output, "String Class Constructors", _ 31 MessageBoxButtons.OK, MessageBoxIcon.Information) 32 End Sub ' Main 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, "

 2002 Prentice Hall. All rights reserved. Outline 6 StringConstructo r.vb

 2002 Prentice Hall. All rights reserved. Outline 7 StringMiscellane ous.vb 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 As String 11 Dim characterArray As Char() 12 Dim i As Integer 13 Dim quotes As String = ChrW(34) string1 = "hello there" 16 characterArray = New Char(5) {} ' output string 19 output = "string1: " & quotes & string1 & quotes ' test Length property 22 output &= vbCrLf & "Length of string1: " & string1.Length ' loop through characters in string1 and display 25 ' reversed 26 output &= vbCrLf & "The string reversed is: " For i = string1.Length - 1 To 0 Step output &= string1.Chars(i) 30 Next ' copy characters from string1 into characterArray 33 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

 2002 Prentice Hall. All rights reserved. Outline 8 StringMiscellane ous.vb 36 For i = 0 To characterArray.Length output &= characterArray(i) 38 Next MessageBox.Show(output, "Demonstrating String" & _ 41 " properties Length and Chars", _ 42 MessageBoxButtons.OK, MessageBoxIcon.Information) 43 End Sub ' Main End Module ' modMiscellaneous Displays contents of characterArray

 2002 Prentice Hall. All rights reserved 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. Outline 10 StringCompare.vb Call to Equals = operator 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 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 String 14 Dim quotes As String = ChrW(34) ' 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 ' test for equality using Equals method 24 If (string1.Equals("hello")) Then 25 output &= "string1 equals " & quotes & "hello" & _ 26 quotes & vbCrLf Else 29 output &= "string1 does not equal " & quotes & _ 30 "hello" & quotes & vbCrLf 31 End If ' test for equality with = 34 If string1 = "hello" Then 35 output &= "string1 equals " & quotes & "hello" & _ Method Equals performs case- sensitive lexicographical comparison Equality operator produces same results as method Equals

 2002 Prentice Hall. All rights reserved. Outline 11 StringCompare.vb Call to Equals Call to CompareTo 36 quotes & vbCrLf 37 Else 38 output &= "string1 does not equal " & quotes & _ 39 "hello" & quotes & vbCrLf 40 End If ' test for equality comparing case 43 If (String.Equals(string3, string4)) Then 44 output &= "string3 equals string4" & vbCrLf 45 Else 46 output &= "string3 does not equal string4" & vbCrLf 47 End If ' 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 MessageBox.Show(output, "Demonstrating string" & _ 62 " comparisons", MessageBoxButtons.OK, _ 63 MessageBoxIcon.Information) 64 End Sub ' Main End Module ' modCompare Shared method Equals compares two String s lexicographically Method CompareTo performs a lexicographical comparison. Returns 0 if String s are equal. Returns –1 if argument String is greater. Returns 1 if calling String is greater

 2002 Prentice Hall. All rights reserved. Outline 12 StringCompare.vb

 2002 Prentice Hall. All rights reserved. Outline 13 StringStartEnd.v b Call to StartsWith Call to EndsWith 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 As String() 10 Dim output As String = "" 11 Dim i As Integer 12 Dim quotes As String = ChrW(34) strings = New String() {"started", "starting", _ 15 "ended", "ending"} ' test every string to see if it starts with "st" 18 For i = 0 To strings.GetUpperBound(0) If strings(i).StartsWith("st") Then 21 output &= quotes & strings(i) & quotes & _ 22 " starts with " & quotes & "st" & quotes & vbCrLf 23 End If Next output &= vbCrLf ' test every string to see if it ends with "ed" 30 For i = 0 To strings.GetUpperBound(0) If strings(i).EndsWith("ed") Then 33 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

 2002 Prentice Hall. All rights reserved. Outline 14 StringStartEnd.v b 35 End If Next MessageBox.Show(output, "Demonstrating StartsWith and" & _ 40 " EndsWith methods", MessageBoxButtons.OK, _ 41 MessageBoxIcon.Information) 42 End Sub ' Main End Module ' modStartEnd

 2002 Prentice Hall. All rights reserved. Outline 15 StringIndexMetho ds.vb Calls to IndexOf Calls to LastIndexOf 1 ' Fig. 15.6: StringIndexMethods.vb 2 ' Using String searching methods. 3 4 Imports System.Windows.Forms 5 6 Module modIndexMethods 7 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} ' test IndexOf to locate a character in a string 14 output &= """c"" is located at index " & _ 15 letters.IndexOf("c"c) output &= vbCrLf & """a"" is located at index " & _ 18 letters.IndexOf("a"c, 1) output &= vbCrLf & """$"" is located at index " & _ 21 letters.IndexOf("$"c, 3, 5) ' test LastIndexOf to find a character in a string 24 output &= vbCrLf & vbCrLf & "Last ""c"" is located at " & _ 25 "index " & letters.LastIndexOf("c"c) output &= vbCrLf & "Last ""a"" is located at index " & _ 28 letters.LastIndexOf("a"c, 25) 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

 2002 Prentice Hall. All rights reserved. Outline 16 StringIndexMetho ds.vb Calls to IndexOf Calls to LastIndexOf Calls to IndexOfAny 33 ' test IndexOf to locate a substring in a string 34 output &= vbCrLf & vbCrLf & """def"" is located at" & _ 35 " index " & letters.IndexOf("def") output &= vbCrLf & """def"" is located at index " & _ 38 letters.IndexOf("def", 7) output &= vbCrLf & """hello"" is located at index " & _ 41 letters.IndexOf("hello", 5, 15) ' test LastIndexOf to find a substring in a string 44 output &= vbCrLf & vbCrLf & "Last ""def"" is located " & _ 45 "at index " & letters.LastIndexOf("def") output &= vbCrLf & "Last ""def"" is located at " & _ 48 letters.LastIndexOf("def", 25) output &= vbCrLf & "Last ""hello"" is located at " & _ 51 "index " & letters.LastIndexOf("hello", 20, 15) ' 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) output &= vbCrLf & "First occurrence of ""c"", ""a"" or " & _ 60 """$"" is located at " & _ 61 letters.IndexOfAny(searchLetters, 7) 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

 2002 Prentice Hall. All rights reserved. Outline 17 StringIndexMetho ds.vb Calls to LastIndexOfAny 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) output &= vbCrLf & "Last occurrence of ""c"", ""a"" or " & _ 74 """$"" is located at " & _ 75 letters.LastIndexOfAny(searchLetters, 1) output &= vbCrLf & "Last occurrence of ""c"", ""a"" or " & _ 78 """$"" is located at " & _ 79 letters.LastIndexOfAny(searchLetters, 25, 5) MessageBox.Show(output, _ 82 "Demonstrating String class index methods") 83 End Sub ' Main 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

 2002 Prentice Hall. All rights reserved. Outline 18 StringIndexMetho ds.vb

 2002 Prentice Hall. All rights reserved. Outline 19 SubString.vb Call to Substring Call to Substring 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 As String = ChrW(34) ' invoke SubString method and pass it one parameter 14 output = "Substring from index 20 to end is " & _ 15 quotes & letters.Substring(20) & quotes & vbCrLf ' invoke SubString method and pass it two parameters 18 output &= "Substring from index 0 to 6 is " & _ 19 quotes & letters.Substring(0, 6) & quotes MessageBox.Show(output, _ 22 "Demonstrating String method Substring") 23 End Sub ' Main 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

 2002 Prentice Hall. All rights reserved. Outline 20 SubConcatination.vb Call to Concat 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 output = "string1 = """ & string1 & """" & _ 14 vbCrLf & "string2 = """ & string2 & """" output &= vbCrLf & vbCrLf & _ 17 "Result of String.Concat(string1, string2) = " & _ 18 String.Concat(string1, string2) MessageBox.Show(output, _ 21 "Demonstrating String method Concat") 22 End Sub ' Main End Module ' modSubConcat Shared method Concat returns a new String object containing the combined characters from both original String s

 2002 Prentice Hall. All rights reserved. Outline 21 StringMiscellane ous.vb Call to Replace Call to ToUpper Call to ToLower 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 As String = ChrW(34) 15 Dim i As Integer output = "string1 = " & quotes & string1 & quotes & _ 18 vbCrLf & "string2 = " & quotes & string2 & quotes & _ 19 vbCrLf & "string3 = " & quotes & string3 & quotes ' 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 ' 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 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

 2002 Prentice Hall. All rights reserved. Outline 22 StringMiscellane ous.vb Call to Trim Call to ToString 33 ' call Trim method 34 output &= vbCrLf & vbCrLf & "string3 after trim = " & _ 35 quotes & string3.Trim() & quotes ' call ToString method 38 output &= vbCrLf & vbCrLf & "string1 = " & _ 39 quotes & string1.ToString() & quotes MessageBox.Show(output, _ 42 "Demonstrating Miscellaneous String Methods") 43 End Sub ' Main 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