Presentation is loading. Please wait.

Presentation is loading. Please wait.

Strings, Characters and Regular Expressions

Similar presentations


Presentation on theme: "Strings, Characters and Regular Expressions"— Presentation transcript:

1 Strings, Characters and Regular Expressions
16 Strings, Characters and Regular Expressions

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 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 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 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 strings 16.6   Locating Characters and Substrings in strings 16.7   Extracting Substrings from strings 16.8   Concatenating strings 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 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 Regular Expression Example   Validating User Input with Regular Expressions Regex methods Replace and Split 16.17 Wrap-Up

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 16.2 Fundamentals of Characters and Strings
“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 before the beginning quotation mark

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 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 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 Outline StringConstructor .cs (2 of 2)

13 Software Engineering Observation 16.1
In most cases, it is not necessary to make a copy of an existing string. All strings 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 16.4 string Indexer, Length Property and CopyTo Method
Facilitates the retrieval of any character in the string Treats a string as an array of chars 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 Outline (1 of 2) Determine the number of characters in string1
StringMethods.cs (1 of 2) Determine the number of characters in string1 Print the corresponding character at the indexer position of string1

16 Outline Copy the characters from a string into a char array (2 of 2)
StringMethods.cs (2 of 2) Determine the number of characters in the array Print the corresponding character at the indexer position of the array

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 16.5 Comparing strings 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 strings 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 Outline StringCompare.cs (1 of 3) Method Equals tests two strings for equality using lexicographical comparison

20 Method CompareTo compares string objects
Operator == also tests two strings for equality using lexicographical comparison Outline StringCompare.cs (2 of 3) static method Equals tests two strings for equality using lexicographical comparison Method CompareTo compares string objects

21 Outline StringCompare.cs (3 of 3)

22 Outline StringStartEnd.cs (1 of 2) Method StartsWith determines if strings starts with specified characters

23 Method EndsWith determines if strings ends with specified characters
Outline Method EndsWith determines if strings ends with specified characters StringStartEnd.cs (2 of 2)

24 16.6 Locating Characters and Substrings in strings
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 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 Outline Method IndexOf finds the first occurrence of substring in letters StringIndexMethods .cs (2 of 4) 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 Outline Method LastIndexOfAny returns the index of the last occurrence of any of the characters in the array StringIndexMethods .cs (3 of 4)

28 Outline StringIndexMethods .cs (4 of 4)

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 16.7 Extracting Substrings from strings
Method Substring Creates and returns a new string by copying part of an existing string

31 Outline Beginning at index 20, copy all the characters from letters
SubString.cs Beginning at index 20, copy all the characters from letters Extract the characters from index 0 to 6 from letters

32 16.8 Concatenating Strings
Method Concat or + Returns a new string containing the combined characters from both original strings

33 Outline Concatenate string2 to string1
SubConcatenation .cs Concatenate string2 to string1 However, string1 is not modified by method Concat

34 16.9 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 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 Use method Trim to return a copy of string3 in which whitespace is eliminated
Outline StringMethods2.cs (2 of 2)

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 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 strings (such as the sharing of one string among multiple references), because it knows these objects will not change.

39 Namespace for class StringBuilder
Outline Namespace for class StringBuilder No-argument constructor creates empty StringBuilder with capacity of 16 characters StringBuilderConst ructor.cs 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

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 Outline Create a new StringBuilder (1 of 2)
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

42 Print corresponding character to the indexer’s position in buffer
Property Length returns the number of characters currently in the buffer Outline Print corresponding character to the indexer’s position in buffer StringBuilderFeatu res.cs (2 of 2)

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 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 Outline StringBuilderAppen d.cs (1 of 2)

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

47 Outline (1 of 2) String literal that contains formatting information
StringBuilderAppen dFormat.cs (1 of 2) String literal that contains formatting information string1’s arguments Combine the string literal and the arguments together

48 Outline Another string literal that contains formatting information
StringBuilderAppen dFormat.cs (2 of 2) Combine the string literal and the argument together

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 Outline StringBuilderInser tRemove.cs (1 of 3)

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 Outline StringBuilderInser tRemove.cs (3 of 3)

53 Outline (1 of 2) Replace “Jane” with “Greg” in builder1
StringBuilderRepla ce.cs (1 of 2) Replace “Jane” with “Greg” in builder1 Replace “g” with “G” in the first 5 characters of builder2

54 Outline StringBuilderRepla ce.cs (2 of 2)

55 16.14 Char Methods Char Simple types (including char) are structs
Represents value types structs derive from ValueType Most methods are static IsDigit IsLetter IsLetterOrDigit IsLower IsUpper ToLower ToUpper IsPunctuation IsSymbol

56 Convert the user’s input to a char
Outline StaticCharMethods. cs (1 of 3) Convert the user’s input to a char

57 Outline Determine whether character is a digit
Determine whether character is a letter Determine whether character is a letter or a digit Determine whether character is a letter or a digit StaticCharMethods. cs (2 of 3) Determine whether character is lowercase and uppercase, respectively Convert character to its uppercase and lowercase, respectively Determine whether character is a punctuation Determine whether character is a symbol

58 (a) Outline (b) (c) StaticCharMethods. cs (3 of 3) (d) (e)

59 16.15 Card Shuffling and Dealing Simulation
This example shows how strings could be used in programs

60 Fields that represents a card
Outline Fields that represents a card Card.cs

61 Outline An array of Cards to represent a deck of cards (1 of 5)
DeckForm.cs (1 of 5) An array of strings to represent the many faces of a card An array of strings to represent the many suits of a card

62 Outline Assign a face and a suit to every card of the deck (2 of 5)
DeckForm.cs (2 of 5) Store the dealt card Display the dealt card Notify user that no cards remain

63 Outline Create a Random object to make shuffle random (3 of 5)
DeckForm.cs (3 of 5) Swap cards for shuffling

64 Outline DeckForm.cs (4 of 5) Shuffle cards

65 (a) Outline DeckForm.cs (5 of 5) (b) (c) (d)

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 Matches

67 Fig. 16.18 | Character classes.

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 Outline Using System.Text.RegularExpressions namespace for finding patterns in the text RegexMatches.cs Create a Regex object with a regular expression string string1 used for finding patterns Find and print out all matches

70 Fig. 16.20 | Quantifiers used in regular expressions.

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 Outline Validate.cs (1 of 7)

73 Outline Validate.cs (2 of 7) The Success property indicates whether the first argument matches the pattern from the second argument

74 Outline Validate.cs (3 of 7) The Success property indicates whether the first argument matches the pattern from the second argument

75 Outline Validate.cs (4 of 7) The Success property indicates whether the first argument matches the pattern from the second argument

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 (a) Outline Validate.cs (6 of 7) (b)

78 (c) Outline Validate.cs (7 of 7) (d)

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 Outline (1 of 2) strings used for finding patterns
RegexSubstitution. cs (1 of 2) strings used for finding patterns Create a Regex object Replaces “*” with “^” in testString1 Replaces “stars” with “carets” in testString1 Replaces every word with “word” Replaces the first 3 digits with “digit”

81 Outline Split the string at commas and have each substring as an element of a string array RegexSubstitution. cs (2 of 2)


Download ppt "Strings, Characters and Regular Expressions"

Similar presentations


Ads by Google