Strings, Characters and Regular Expressions

Slides:



Advertisements
Similar presentations
Purpose : To convert this string to a new character array. Return Type : char[ ] Parameters : none Declaration : public char[ ] toCharArray() Returns.
Advertisements

 2002 Prentice Hall. All rights reserved. 1 Chapter 15 – Strings, Characters and Regular Expressions Outline 15.1Introduction 15.2 Fundamentals of Characters.
Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 1 Chapter 9 Strings.
1 Strings and Text I/O. 2 Motivations Often you encounter the problems that involve string processing and file input and output. Suppose you need to write.
Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 122 – Data Structures Characters and Strings.
Java Programming Strings Chapter 7.
©2004 Brooks/Cole Chapter 7 Strings and Characters.
 2008 Pearson Education, Inc. All rights reserved JavaScript: Introduction to Scripting.
Chapter 9 Characters and Strings. Topics Character primitives Character Wrapper class More String Methods String Comparison String Buffer String Tokenizer.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 9 Characters and Strings (sections ,
Fundamental Programming Structures in Java: Strings.
 2006 Pearson Education, Inc. All rights reserved Strings, Characters and Regular Expressions.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 9 Characters and Strings (sections ,
 Pearson Education, Inc. All rights reserved Strings, Characters and Regular Expressions.
JavaScript, Third Edition
 Pearson Education, Inc. All rights reserved Arrays.
Lesson 3 – Regular Expressions Sandeepa Harshanganie Kannangara MBCS | B.Sc. (special) in MIT.
 2007 Pearson Education, Inc. All rights reserved C Characters and Strings.
Java How to Program, 9/e © Copyright by Pearson Education, Inc. All Rights Reserved.
Array String String Builder. Arrays Arrays are collections of several elements of the same type E.g. 100 integers, 20 strings, 125 students, 12 dates,
CSCI 3327 Visual Basic Chapter 12: Strings, Characters and Regular Expressions UTPA – Fall 2011.
Characters The data type char represents a single character in Java. –Character values are written as a symbol: ‘a’, ‘)’, ‘%’, ‘A’, etc. –A char value.
ASP.NET Programming with C# and SQL Server First Edition Chapter 5 Manipulating Strings with C#
An Introduction to Java Programming and Object-Oriented Application Development Chapter 7 Characters, Strings, and Formatting.
When you read a sentence, your mind breaks it into tokens—individual words and punctuation marks that convey meaning. Compilers also perform tokenization.
Chapter 7: Characters, Strings, and the StringBuilder.
1 Textual Data Many computer applications manipulate textual data word processors web browsers online dictionaries.
CSCI 3327 Visual Basic Chapter 12: Strings, Characters and Regular Expressions UTPA – Fall 2011.
String String Builder. System.String string is the alias for System.String A string is an object of class string in the System namespace representing.
 2008 Pearson Education, Inc. All rights reserved JavaScript: Introduction to Scripting.
C++ for Engineers and Scientists Second Edition Chapter 7 Completing the Basics.
Strings and Related Classes String and character processing Class java.lang.String Class java.lang.StringBuffer Class java.lang.Character Class java.util.StringTokenizer.
Strings, Characters, and Regular Expressions Session 10 Mata kuliah: M0874 – Programming II Tahun: 2010.
 2009 Pearson Education, Inc. All rights reserved. 1 Ch.18 Strings, Characters [and Regular Expressions] Many slides modified by Prof. L. Lilien (even.
String and StringBuffer classes
28 Formatted Output.
Chapter 1.2 Introduction to C++ Programming
C Characters and Strings
Introduction to Classes and Objects
C Characters and Strings
Strings, Characters and Regular Expressions
Strings, Characters and Regular Expressions
Strings, StringBuilder, and Character
String Processing Upsorn Praphamontripong CS 1110
Strings and Characters
String String Builder.
Chapter 19 PHP Part II Credits: Parts of the slides are based on slides created by textbook authors, P.J. Deitel and H. M. Deitel by Prentice Hall ©
Primitive Types Vs. Reference Types, Strings, Enumerations
Lecture 4 Using Classes Richard Gesick.
String Objects & its Methods
String and StringBuilder
Chapter 7: Strings and Characters
MSIS 655 Advanced Business Applications Programming
Chapter 3 Introduction to Classes, Objects Methods and Strings
Operator Overloading; String and Array Objects
Pointers and Pointer-Based Strings
Operator Overloading; String and Array Objects
Strings and Characters
Class string and String Stream Processing
Topics Basic String Operations String Slicing
Object Oriented Programming in java
JavaScript: Objects.
String Processing 1 MIS 3406 Department of MIS Fox School of Business
Visual Programming COMP-315
Topics Basic String Operations String Slicing
String Manipulation.
Topics Basic String Operations String Slicing
Unit-2 Objects and Classes
What We Want To Do User enters: Mary Smith
Presentation transcript:

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

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

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

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.

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

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

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

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 use @ before the beginning quotation mark

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.

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…” );

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

Outline StringConstructor .cs (2 of 2)

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.

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

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

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

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.

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

Outline StringCompare.cs (1 of 3) Method Equals tests two strings for equality using lexicographical comparison

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

Outline StringCompare.cs (3 of 3)

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

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

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

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

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

Outline Method LastIndexOfAny returns the index of the last occurrence of any of the characters in the array StringIndexMethods .cs (3 of 4)

Outline StringIndexMethods .cs (4 of 4)

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.

16.7 Extracting Substrings from strings Method Substring Creates and returns a new string by copying part of an existing string

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

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

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

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

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

Use method Trim to return a copy of string3 in which whitespace is eliminated Outline StringMethods2.cs (2 of 2)

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

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.

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

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

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

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)

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).

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

Outline StringBuilderAppen d.cs (1 of 2)

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

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

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

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

Outline StringBuilderInser tRemove.cs (1 of 3)

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

Outline StringBuilderInser tRemove.cs (3 of 3)

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

Outline StringBuilderRepla ce.cs (2 of 2)

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

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

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

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

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

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

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

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

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

Outline DeckForm.cs (4 of 5) Shuffle cards

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

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

Fig. 16.18 | Character classes.

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

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

Fig. 16.20 | Quantifiers used in regular expressions.

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

Outline Validate.cs (1 of 7)

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

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

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

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

(a) Outline Validate.cs (6 of 7) (b)

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

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

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”

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