Title: Strings, Characters, and Regular Expressions
1Strings, Characters, andRegular Expressions
2String Theory
Assignment
3Fundamentals of Characters Strings
- A character constant is a character that is
represented as an integer - value and this value is known as a character
code. - z corresponds to the integer value 122. \n
corresponds - to integer value 10.
- The unicode character set is an international
character set that extends ASCII. - See Appendix G in your textbook.
- A string is a series of characters treated as a
single unit. - A string is an object of class String in the
System namespace. - String constants are sequences of characters in
double quotation marks - Gabe Lujan
- (972) 599 1698
- Weve used string color blue
- string file C\\Inetpub\\wwwroot\\Login\\login
.mdb - string file _at_C\Inetpub\wwwroor\Login\login.m
db
4Fundamentals of Characters Strings
- Class String provides eight constructors for
initializing strings. - Code Example for String Constructors
Note ArgumentOutOfRangeException
5String Constructors
- Examples
- string mystr, string1, string2, string3,
string4, output - char characterArray a, n, n,i,
v,e,r,s,a,r,y - mystr This is my string.
- string1 mystr
- string2 new string( characterArray )
- string3 new string( characterArray 6, 3 )
- string4 new string( C, 5)
- output string1 string2
-
-
6String Indexer, Length Property, CopyTo Method
- This code example illustrates the use of the
string indexer. It - facilitates the retrieval of any character in
the string. - The Length property returns the length of a
string. - The CopyTo method copies a specified number of
characters from a string into a character array.
7Comparing Strings
- In C, one string can be greater than or less
than another string. The - alphabetic symboloy of a character string
ultimately maps to an integer - representation for computer representation.
- Class string provides several ways to compare
strings. - Equals
- CompareTo
-
- Equals uses a lexicographical comparison
- a Unicode character by Unicode character
- comparison.
- StringCompare Code Example
- Code Example shows how to test
- if a string starts with or ends with
- a given string.
8GetHashCode
- Often it is necessary to store strings and other
data types in a manner that - enables the information to be found quickly.
- Hash tables make information look-up easy. A hash
table stores an - object by performing a special calculation on
that object, which produces a hash code. - The object then is stored at a location in the
hash table determined by the calculated hash
code. - When a program needs to retrieve the information,
the same calculation is performed, generating the
same hash code. - Class Object defines method GetHashCode. Class
String overrides this method to provide a
hashcode distribution based on the contents of a
string. - GetHashCode Example
9Locating Characters and Substrings in Strings
- Code Example
- The input string is abcdefghijklmabcdefghijklm
- What are the outputs from the following methods
- IndexOf
- IndexOfAny
- LastIndexOf
- LastIndexOfAny
In the LastIndexOf method, the search moves from
the end of the string toward the start of the
string.
10Extracting Substrings
- Class String provides two Substring methods,
which are used to - create a new string by copying part of an
existing string. - 2. Code Example showing two uses of Substring()
method.
Input text string is abcdefghijklmabcdefghijklm
11Concatenating Strings
- The operator is one way of concatenating
strings. - The static method Concat of class String
concatenates two - strings and returns a new string. The input
strings are not - modified.
- Code Sample
12Miscellaneous String Methods
- Note the usages of the following String methods
- Replace
- ToLower
- ToUpper
- Trim
- ToString
- Code Sample
13Class StringBuilder
- The String class provides many capabilities for
processing strings. - However, a strings contents can never change.
Operations that - concatenate strings are really assigning string
references to - newly created strings. For instance, creates
a new string and - assigns the initial string reference to the
newly created string. - Class StringBuilder comes from System.Text
namespace. - Used to create mutable strings.
- StringBuilder can store a fixed amount of
characters, but their capacities can also expand
to accommodate additional characters. - Concatenation can be achieved using methods
Append() and AppendFormat(). - Code Sample Showing StringBuilder Constructors
14StringBuilder Indexer, Length Capacity
Properties, and EnsureCapacity Method
- StringBuilder provides Length and Capacity
properties to return - the number of characters in a StringBuilder and
the number - of characters that a StringBuilder can store
without allocating - more memory. These properties can also increase
or decrease - the capacity of the StringBuilder.
- Method EnsureCapacity allows programmers to
guarantee that a StringBuilder has a capacity
that reduces the number of times the capacity
must be increased. It doubles the StringBuilder
instances current capacity. If the doubled value
is greater than the value that the programmer
wishes to ensure, it becomes the new capacity.
Otherwise, EnsureCapacity alters the capacity to
make it one more than the requested number, - Example If the current capacity is 17 and we
wish to make it 40, 17 x 2 lt 40, so the call
will result in a new capacity of 41. If the
current capacity is 23 and we wish to make it 40,
23 will be multiplied by 2 to result in a new
capacity of 46. - Code Example
15StringBuilder Append AppendFormat Methods
- Class StringBuilder provides 19 overloaded Append
methods that - allow various data-type values to be added to
the end of a - StringBuilder.
- C provides versions for each of the primitive
data types and for character arrays. - Each of the methods takes an argument, converts
it to a string, and appends it to the
StringBuilder. - Code Example
- AppendFormat
16StringBuilder Insert, Remove, Replace Methods
- StringBuilder provides 18 overloaded Insert
methods to allow various datatype values - to be inserted at any position in a
StringBuilder. Versions are provided for the - primitive data types, character arrays, strings
and objects. - Each method takes its second argument, converts
it to a string, and inserts the string - into the StringBuilder in front of the index
specified by the first argument. If the index is
wrong, the program will throw an
ArgumentOutOfRangeException. - Class StringBuilder provides method Remove for
deleting any portion of a StringBuilder. The two
arguments include the index at which to start the
remove and the number of characters to delete. - Insert and Remove Code Example
- The Replace method searches for a specified
- string or character and substitutes another
- string or character in its place.
- 6. Code Example for Replace
17Char Methods
- C provides a data type called a structure that
is similar to a class. Structures - are a value type and they include methods and
properties. - 2. Structures are created using the keyword
struct. - The primitive datatypes such as Int, Char, and
Float are aliases for structures from class
ValueType - Some Char methods
- IsDigit()
- IsLetter()
- IsLetterOrDigit()
- IsLower()
- IsPunctuation()
- IsSymbol()
- Code Example
18Card Shuffling Dealing Simulation
- This program example uses random number
generation to develop a program - that simulates the shuffling and dealing of
cards. - What to look for
- a) The Card constructor
- b) The deck data type how each of the 52 cards
is constructed. - c) The shuffle method
- d) The deal method.
- Card Code
- Deck of Cards
19Regular Expressions
- Regular expressions are specially formatted
strings used to find patterns in text - and can be useful during information validation.
- Example Zip code must contain 5 digits, last
name must start with a - capital letter.
- Class Regex (Syste.Text.RegularExpressions
namespace) represents an immutable expression. It
contains static methods so no class
instantiation is needed. - Class Regex provides a method Match which returns
an object of class Match that represents a single
regular expression match. - Method Matches finds all matches of a regular
expression in an arbitrary string and returns a
MatchCollection object. - A word-character is any alphanumeric character or
underscore. A whitespace character is a space,
tab, carriage return, newline, or form feed. A
digit is any numeric character.
20Regular Expressions
- Regular expression code example
- This code example takes birthdays and tries to
match them to a regular expression. The
expression matches only birthdays that do not
occur in April and that belong to people whose
names begin with J.
- What to look for
- The . character matches any single character
except a newline character. - . matches any number of unspecified
characters. - will match one or more occurances. A will
match A and and empty string. - aeiou can be used to match any vowel.
- 0-35-9 matches only digits in the ranges
specified by the pattern. This pattern matches
any digit other than 4. - - the expression accepts any character other
than the ones indicated. 4 is not the same
as 0-35-9 Why? The former also matches any
non-digit.
21Regular Expressions
- 7. (?) matches zero or one occurrences of the
expression that it quantifies. - 8. (n) matches exactly n occurrences of the
expression it quantifies - 9. (n,) matches at least n occurrences.
- 10. (n,m) matches between n and m occurrences.
- 11. Code Example
- What to Look For
- The program checks to see if an entire string
- conforms to a regular expression. and
- characters match the positions at the beginning
- and end of a string. This forces the regular
- expression to match the entire string and not
- return a match if a substring matches
successfully. - \s matches any white space
- \d5 matches any five digits.
- matches the expression to its left or to its
right. - Hi (John Jane) matches Hi John and Hi Jane
22Regular Expressions
- Sometimes it is necessary to replace parts of a
string with another or split a string according
to a regular expression. - Regex provides static instance methods of Replace
and Split. - Code Example
23Assignment
- Write a C GUI program using a menu that replaces
all of the - occurrences of a word in a file with another
word. Display - the file before and after using Notepad. The
program - should prompt the user for the input file name
and - the string to be substituted for.