Presentation is loading. Please wait.

Presentation is loading. Please wait.

 2006 Pearson Education, Inc. All rights reserved. 1 16 Strings, Characters and Regular Expressions.

Similar presentations


Presentation on theme: " 2006 Pearson Education, Inc. All rights reserved. 1 16 Strings, Characters and Regular Expressions."— Presentation transcript:

1  2006 Pearson Education, Inc. All rights reserved. 1 16 Strings, Characters and Regular Expressions

2  2006 Pearson Education, Inc. All rights reserved. 2 The chief defect of Henry King Was chewing little bits of string. — Hilaire Belloc Vigorous writing is concise. A sentence should contain no unnecessary words, a paragraph no unnecessary sentences. — William Strunk, Jr. I have made this letter longer than usual, because I lack the time to make it short. — Blaise Pascal

3  2006 Pearson Education, Inc. All rights reserved. 3 The difference between the almost-right word & the right word is really a large matter—it’s the difference between the lightning bug and the lightning. — Mark Twain Mum’s the word. — Miguel de Cervantes, Don Quixote de la Mancha

4  2006 Pearson Education, Inc. All rights reserved. 4 OBJECTIVES In this chapter you will learn:  To create and manipulate immutable character string objects of class string.  To create and manipulate mutable character string objects of class StringBuilder.  To manipulate character objects of struct Char.  To use regular expressions in conjunction with classes Regex and Match.

5  2006 Pearson Education, Inc. All rights reserved. 5 16.1 Introduction 16.2 Fundamentals of Characters and Strings 16.3 string Constructors 16.4 string Indexer, Length Property and CopyTo Method 16.5 Comparing string s 16.6 Locating Characters and Substrings in string s 16.7 Extracting Substrings from string s 16.8 Concatenating string s 16.9 Miscellaneous string Methods 16.10 Class StringBuilder 16.11 Length and Capacity Properties, EnsureCapacity Method and Indexer of Class StringBuilder 16.12 Append and AppendFormat Methods of Class StringBuilder

6  2006 Pearson Education, Inc. All rights reserved. 6 16.13 Insert, Remove and Replace Methods of Class StringBuilder 16.14 Char Methods 16.15 Card Shuffling and Dealing Simulation 16.16 Regular Expressions and Class Regex 16.16.1 Regular Expression Example 16.16.2 Validating User Input with Regular Expressions 16.16.3 Regex methods Replace and Split 16.17 Wrap-Up

7  2006 Pearson Education, Inc. All rights reserved. 7 16.1 Introduction FCL’s string and character processing capabilities – string ’s constructors and methods – StringBuilder from System.Text namespace Builds strings dynamically – Regex and Match from the System.Text.RegularExpressions namespace Manipulate by patterns

8  2006 Pearson Education, Inc. All rights reserved. 8 16.2 Fundamentals of Characters and Strings Characters – “Building blocks” of C# source programs – Character constant A character that is represented as an integer value – Unicode character set International character set String – Immutable – Series of characters treated as single unit – May include letters, digits, special characters – String literals Sequence of characters – From System namespace – To interpret all characters literally use @ before the beginning quotation mark

9  2006 Pearson Education, Inc. All rights reserved. 9 Performance Tip 16.1 If there are multiple occurrences of the same string literal object in an application, a single copy of the string literal object will be referenced from each location in the program that uses that string literal. It is possible to share the object in this manner, because string literal objects are implicitly constant. Such sharing conserves memory.

10  2006 Pearson Education, Inc. All rights reserved. 10 16.3 string Constructors string Constructors – Can initialize string as if it was a primitive type Ex: string example = “I see…”; – Can initialize string in the same way as a normal class (Eight constructors) Ex: string example = new string ( “I see…” );

11  2006 Pearson Education, Inc. All rights reserved. 11 Outline StringConstructor.cs (1 of 2) Assigns string literal to string reference originalString Set string1 to have the same string literal as originalString One-argument constructor creates a string that contains a copy of the characters in the array argument Three-argument constructor creates a string that contains a copy of partial characters in the array argument Two-argument constructor creates a string that contains the character argument repeated a specified numbers of time

12  2006 Pearson Education, Inc. All rights reserved. 12 Outline StringConstructor.cs (2 of 2)

13  2006 Pearson Education, Inc. All rights reserved. 13 Software Engineering Observation 16.1 In most cases, it is not necessary to make a copy of an existing string. All string s are immutable—their character contents cannot be changed after they are created. Also, if there are one or more references to a string (or any object for that matter), the object cannot be reclaimed by the garbage collector.

14  2006 Pearson Education, Inc. All rights reserved. 14 16.4 string Indexer, Length Property and CopyTo Method string indexer – Facilitates the retrieval of any character in the string – Treats a string as an array of char s Return the character at the specific position in the string Length property – Returns the length of the string CopyTo Method – Copies a specified number of characters into a char array

15  2006 Pearson Education, Inc. All rights reserved. 15 Outline StringMethods.cs (1 of 2) Determine the number of characters in string1 Print the corresponding character at the indexer position of string1

16  2006 Pearson Education, Inc. All rights reserved. 16 Outline StringMethods.cs (2 of 2) Determine the number of characters in the array Print the corresponding character at the indexer position of the array Copy the characters from a string into a char array

17  2006 Pearson Education, Inc. All rights reserved. 17 Common Programming Error 16.1 Attempting to access a character that is outside a string ’s bounds (i.e., an index less than 0 or an index greater than or equal to the string ’s length) results in an IndexOutOfRangeException.

18  2006 Pearson Education, Inc. All rights reserved. 18 16.5 Comparing string s Comparing String objects – Method Equals or == Determine if the strings are the same Returns bool value Uses a lexicographical comparison – The integer Unicode value that represent each character in each string are compared – Method CompareTo Returns 0 if string s are equal Returns negative value if the string invoked is less than the string that is passed in Returns positive value if the string invoked is greater than the string that is passed in – Method StartsWith Determines if string instance starts with the string text passed to it as an argument – Method EndWith Determines if string instance ends with the string text passed to it as an argument

19  2006 Pearson Education, Inc. All rights reserved. 19 Outline StringCompare.cs (1 of 3) Method Equals tests two strings for equality using lexicographical comparison

20  2006 Pearson Education, Inc. All rights reserved. 20 Outline StringCompare.cs (2 of 3) Operator == also tests two strings for equality using lexicographical comparison static method Equals tests two strings for equality using lexicographical comparison Method CompareTo compares string objects

21  2006 Pearson Education, Inc. All rights reserved. 21 Outline StringCompare.cs (3 of 3)

22  2006 Pearson Education, Inc. All rights reserved. 22 Outline StringStartEnd.cs (1 of 2) Method StartsWith determines if strings starts with specified characters

23  2006 Pearson Education, Inc. All rights reserved. 23 Outline StringStartEnd.cs (2 of 2) Method EndsWith determines if strings ends with specified characters

24  2006 Pearson Education, Inc. All rights reserved. 24 16.6 Locating Characters and Substrings in string s Search for characters in string – Method IndexOf Returns the index of first occurrence of a character or substring; -1 if not found – Method IndexOfAny Same as IndexOf excepts it takes in an array of characters and returns the index of the first occurrence of any of the characters in the array – Method LastIndexOf Returns the index of last occurrence of a character or substring; -1 if not found – Method LastIndexOfAny Same as LastIndexOf excepts it takes in an array of characters and returns the index of the last occurrence of any of the characters in the array

25  2006 Pearson Education, Inc. All rights reserved. 25 Outline StringIndexMethods.cs (1 of 4) Method IndexOf finds the first occurrence of character in letters Method LastIndexOf finds the last occurrence of character in letters

26  2006 Pearson Education, Inc. All rights reserved. 26 Outline StringIndexMethods.cs (2 of 4) Method IndexOf finds the first occurrence of substring in letters Method LastIndexOf the finds last occurrence of substring in letters Method IndexOfAny returns the index of the first occurrence of any of the characters in the array

27  2006 Pearson Education, Inc. All rights reserved. 27 Outline StringIndexMethods.cs (3 of 4) Method LastIndexOfAny returns the index of the last occurrence of any of the characters in the array

28  2006 Pearson Education, Inc. All rights reserved. 28 Outline StringIndexMethods.cs (4 of 4)

29  2006 Pearson Education, Inc. All rights reserved. 29 Common Programming Error 16.2 In the overloaded methods LastIndexOf and LastIndexOfAny that take three parameters, the second argument must be greater than or equal to the third. This might seem counterintuitive, but remember that the search moves from the end of the string toward the start of the string.

30  2006 Pearson Education, Inc. All rights reserved. 30 16.7 Extracting Substrings from string s Method Substring – Creates and returns a new string by copying part of an existing string

31  2006 Pearson Education, Inc. All rights reserved. 31 Outline SubString.cs Beginning at index 20, copy all the characters from letters Extract the characters from index 0 to 6 from letters

32  2006 Pearson Education, Inc. All rights reserved. 32 16.8 Concatenating Strings Method Concat or + – Returns a new string containing the combined characters from both original string s

33  2006 Pearson Education, Inc. All rights reserved. 33 Outline SubConcatenation.cs Concatenate string2 to string1 However, string1 is not modified by method Concat

34  2006 Pearson Education, Inc. All rights reserved. 34 16.9 Miscellaneous string Methods Miscellaneous string methods – Method Replace Returns a new string replacing every occurrence of the specified phrase with another phrase in the string – Method ToLower Returns a new lower cased version of the string – Method ToUpper Returns a new upper cased version of the string – Method Trim Remove all white space characters from the string

35  2006 Pearson Education, Inc. All rights reserved. 35 Outline StringMethods2.cs (1 of 2) Use method Replace to return a copy of string1 in which every occurrence of ‘ e ’ is replaced with ‘ E ’ Use method ToUpper to return a copy of string1 in which every character is uppercase Use method ToLower to return a copy of string2 in which every character is lowercase

36  2006 Pearson Education, Inc. All rights reserved. 36 Outline StringMethods2.cs (2 of 2) Use method Trim to return a copy of string3 in which whitespace is eliminated

37  2006 Pearson Education, Inc. All rights reserved. 37 16.10 Class StringBuilder Class StringBuilder – Used to create and manipulate dynamic string information – Every StringBuilder can store the number of characters specified by its capacity Exceeding the capacity of a StringBuilder makes the capacity expand to accommodate the additional characters The default initial capacity is 16 characters – 6 overloaded constructors

38  2006 Pearson Education, Inc. All rights reserved. 38 Performance Tip 16.2 Objects of class string are immutable (i.e., constant strings), whereas object of class StringBuilder are mutable. C# can perform certain optimizations involving string s (such as the sharing of one string among multiple references), because it knows these objects will not change.

39  2006 Pearson Education, Inc. All rights reserved. 39 Outline StringBuilderConst ructor.cs No-argument constructor creates empty StringBuilder with capacity of 16 characters One-argument constructor creates empty StringBuilder with capacity of specified ( 10 ) characters One-argument constructor creates StringBuilder with string “ hello ” and capacity of 16 characters Namespace for class StringBuilder

40  2006 Pearson Education, Inc. All rights reserved. 40 16.11 Length and Capacity Properties, EnsureCapacity Method and Indexer of Class StringBuilder Length Property – Return number of characters currently in the StringBuilder Capacity Property – Return number of characters that the StringBuilder can store without allocating more memory EnsureCapacity Method – Reduce the number of times the StringBuilder’s capacity can be increased Indexers is like that of string

41  2006 Pearson Education, Inc. All rights reserved. 41 Outline StringBuilderFeatu res.cs (1 of 2) Property Length returns the number of characters currently in the buffer Property Capacity returns the number of characters that buffer can store without allocating more memory Use method EnsureCapacity to set capacity to 75 Use property Length to set length to 10 Create a new StringBuilder

42  2006 Pearson Education, Inc. All rights reserved. 42 Outline StringBuilderFeatu res.cs (2 of 2) Property Length returns the number of characters currently in the buffer Print corresponding character to the indexer’s position in buffer

43  2006 Pearson Education, Inc. All rights reserved. 43 Common Programming Error 16.3 Assigning null to a string reference can lead to logic errors if you attempt to compare null to an empty string. The keyword null is a value that represents a null reference (i.e., a reference that does not refer to an object), not an empty string (which is a string object that is of length 0 and contains no characters).

44  2006 Pearson Education, Inc. All rights reserved. 44 16.12 Append and AppendFormat Methods of Class StringBuilder Method Append – Appends the string representation to the end the StringBuilder Method AppendFormat – Converts a string to a specified format, then appends it to the StringBuilder

45  2006 Pearson Education, Inc. All rights reserved. 45 Outline StringBuilderAppen d.cs (1 of 2)

46  2006 Pearson Education, Inc. All rights reserved. 46 Outline StringBuilderAppen d.cs (2 of 2) Append boolean, char, int, long, float and double Append string “ hello ” to StringBuilder Append string “ good bye ” Append “ a b c d e f ” Append “ a b c ” Append a space character to StringBuilder Print out results

47  2006 Pearson Education, Inc. All rights reserved. 47 Outline StringBuilderAppen dFormat.cs (1 of 2) String literal that contains formatting information string1 ’s arguments Combine the string literal and the arguments together

48  2006 Pearson Education, Inc. All rights reserved. 48 Outline StringBuilderAppen dFormat.cs (2 of 2) Another string literal that contains formatting information Combine the string literal and the argument together

49  2006 Pearson Education, Inc. All rights reserved. 49 16.13 Insert, Remove and Replace Methods of Class StringBuilder Method Insert – Allow various types of data to be inserted at any position Method Remove – Delete any portion of StringBuilder Method Replace – Searches for a specified string or character and substitutes another string or character in its place

50  2006 Pearson Education, Inc. All rights reserved. 50 Outline StringBuilderInser tRemove.cs (1 of 3)

51  2006 Pearson Education, Inc. All rights reserved. 51 Outline StringBuilderInser tRemove.cs (2 of 3) Use method Insert to insert data in beginning of StringBuilder Use method Remove to remove character from index 10 in StringBuilder Remove characters from indices 4 through 7

52  2006 Pearson Education, Inc. All rights reserved. 52 Outline StringBuilderInser tRemove.cs (3 of 3)

53  2006 Pearson Education, Inc. All rights reserved. 53 Outline StringBuilderRepla ce.cs (1 of 2) Replace “Jane” with “Greg” in builder1 Replace “g” with “G” in the first 5 characters of builder2

54  2006 Pearson Education, Inc. All rights reserved. 54 Outline StringBuilderRepla ce.cs (2 of 2)

55  2006 Pearson Education, Inc. All rights reserved. 55 16.14 Char Methods Char – Simple types (including char ) are struct s Represents value types – struct s derive from ValueType – Most methods are static IsDigit IsLetter IsLetterOrDigit IsLower IsUpper ToLower ToUpper IsPunctuation IsSymbol

56  2006 Pearson Education, Inc. All rights reserved. 56 Outline StaticCharMethods. cs (1 of 3) Convert the user’s input to a char

57  2006 Pearson Education, Inc. All rights reserved. 57 Outline StaticCharMethods. cs (2 of 3) Determine whether character is a digit Determine whether character is lowercase and uppercase, respectively Convert character to its uppercase and lowercase, respectively Determine whether character is a letter Determine whether character is a letter or a digit Determine whether character is a punctuation Determine whether character is a symbol

58  2006 Pearson Education, Inc. All rights reserved. 58 Outline StaticCharMethods. cs (3 of 3) (a) (b)(c) (d)(e)

59  2006 Pearson Education, Inc. All rights reserved. 59 16.15 Card Shuffling and Dealing Simulation This example shows how string s could be used in programs

60  2006 Pearson Education, Inc. All rights reserved. 60 Outline Card.cs Fields that represents a card

61  2006 Pearson Education, Inc. All rights reserved. 61 Outline DeckForm.cs (1 of 5) An array of Card s to represent a deck of cards An array of string s to represent the many faces of a card An array of string s to represent the many suits of a card

62  2006 Pearson Education, Inc. All rights reserved. 62 Outline DeckForm.cs (2 of 5) Assign a face and a suit to every card of the deck Store the dealt card Display the dealt card Notify user that no cards remain

63  2006 Pearson Education, Inc. All rights reserved. 63 Outline DeckForm.cs (3 of 5) Create a Random object to make shuffle random Swap cards for shuffling

64  2006 Pearson Education, Inc. All rights reserved. 64 Outline DeckForm.cs (4 of 5) Shuffle cards

65  2006 Pearson Education, Inc. All rights reserved. 65 Outline DeckForm.cs (5 of 5) (a) (b) (c) (d)

66  2006 Pearson Education, Inc. All rights reserved. 66 16.16 Regular Expressions and Class Regex Regex Class – From namespace System.Text.RegularExpressions – Represents an immutable regular expression Specially formatted strings – Method Match Returns an object of class Match that represents a single regular expression match – Method Matches Finds all matches of a regular expression in a string and returns an object of the class MatchCollection object containing all the Match es

67  2006 Pearson Education, Inc. All rights reserved. 67 Fig. 16.18 | Character classes.

68  2006 Pearson Education, Inc. All rights reserved. 68 16.16.1 Regular Expression Example The dot character “.” matches any single character except a newline character – When the dot character is followed by an asterisk, the regular expression matches any number of unspecified character except newlines Range of characters are represented by placing a dash between two characters Can specify that pattern should match anything other than the characters in the brackets using “^” – Ex: [^4] matches any non-digit and digits other than 4 All qualifiers are greedy – Will match as many occurrences of the pattern as possible – If qualifier is followed by a question mark, it becomes lazy Will match as few occurrences as possible

69  2006 Pearson Education, Inc. All rights reserved. 69 Outline RegexMatches.cs Using System.Text.RegularExpressions namespace for finding patterns in the text Create a Regex object with a regular expression string string1 used for finding patterns Find and print out all matches

70  2006 Pearson Education, Inc. All rights reserved. 70 Fig. 16.20 | Quantifiers used in regular expressions.

71  2006 Pearson Education, Inc. All rights reserved. 71 16.16.2 Validating User Input with Regular Expressions Match Property Success – Indicates whether there was a match "|" matches the expression to its left or to its right Parentheses can be used to group parts of a regular expression Quantifiers may be applied to patterns enclosed in parentheses to create more complex regular expressions

72  2006 Pearson Education, Inc. All rights reserved. 72 Outline Validate.cs (1 of 7)

73  2006 Pearson Education, Inc. All rights reserved. 73 Outline Validate.cs (2 of 7) The Success property indicates whether the first argument matches the pattern from the second argument

74  2006 Pearson Education, Inc. All rights reserved. 74 Outline Validate.cs (3 of 7) The Success property indicates whether the first argument matches the pattern from the second argument

75  2006 Pearson Education, Inc. All rights reserved. 75 Outline Validate.cs (4 of 7) The Success property indicates whether the first argument matches the pattern from the second argument

76  2006 Pearson Education, Inc. All rights reserved. 76 Outline Validate.cs (5 of 7) The Success property indicates whether the first argument matches the pattern from the second argument Hides the form Terminate program

77  2006 Pearson Education, Inc. All rights reserved. 77 Outline Validate.cs (6 of 7) (a) (b)

78  2006 Pearson Education, Inc. All rights reserved. 78 Outline Validate.cs (7 of 7) (c) (d)

79  2006 Pearson Education, Inc. All rights reserved. 79 16.16.3 Regex Methods Replace and Split Method Replace – Replaces text in a string with new text wherever the original string matches a regular expression Method Split – Divides a string into several substrings – The original string is broken at delimiters that match a specified regular expression – Returns an array containing the substrings

80  2006 Pearson Education, Inc. All rights reserved. 80 Outline RegexSubstitution. cs (1 of 2) Create a Regex object string s u sed for finding patterns Replaces “*” with “^” in testString1 Replaces “stars” with “carets” in testString1 Replaces every word with “word” Replaces the first 3 digits with “digit”

81  2006 Pearson Education, Inc. All rights reserved. 81 Outline RegexSubstitution. cs (2 of 2) Split the string at commas and have each substring as an element of a string array


Download ppt " 2006 Pearson Education, Inc. All rights reserved. 1 16 Strings, Characters and Regular Expressions."

Similar presentations


Ads by Google