Chapter 2 Part 2
Chapter 2 Part 2
Chapter 2 - Part 2
Contents
birthday
day
CCCCC
Cont.
● Line 24 assigns to string2 a new string, using the String constructor that takes a
character array as an argument.
● The new string contains a copy of the characters in array characterArray.
● Line 25 assigns to string3 a new string, using the String constructor that takes a char
array and two int arguments. The second argument specifies the starting index
position (the offset) from which characters in the array are copied. The third
argument specifies the number of characters (the count) to be copied from the
specified starting position in the array.
● Line 26 assigns to string4 a new string, using the String constructor that takes as
arguments a character and an int specifying the number of times to repeat that
character in the string.
Cont.
● Method Equals (inherited by String from class Object) tests any two
objects for equality (i.e., checks whether the objects contain identical
contents).
● The method returns true if the objects are equal and false otherwise.
● Method Equals compares the numeric Unicode values that represent
the characters in each string.
● A comparison of the string "hello" with the string "HELLO" would
return false, because the numeric representations of lowercase
letters are different from the numeric representations of
corresponding uppercase letters.
Demo App
Compare strings using CompareTo()
● The two methods shows how to test whether a string instance begins or ends with a
given string.
● Method StartsWith determines whether a string instance starts with the string text
passed to it as an argument.
● Method EndsWith determines whether a string instance ends with the string text passed
to it as an argument.
● The Methods return either true or false accordingly.
Demo App
Locating Strings and Substrings in Strings
char c = Convert.ToChar(textBox6.Text);
string s6 = textBox5.Text;
if(comboBox4.SelectedIndex ==0)
MessageBox.Show( s6.IndexOf(c).ToString());
}
Demo App IndexOf(char, starting_index<int>)
if(comboBox4.SelectedIndex ==1)
MessageBox.Show(s6.IndexOf(c, 1).ToString());
}
LastIndexOf()
char c = Convert.ToChar(textBox6.Text);
string s6 = textBox5.Text;
if(comboBox4.SelectedIndex ==0) {
MessageBox.Show( s6.IndexOf(c).ToString());
if(comboBox4.SelectedIndex ==1) {
MessageBox.Show(s6.IndexOf(c, 1).ToString()); }
if (comboBox4.SelectedIndex ==3) {
MessageBox.Show(null,
s6.LastIndexOf(c).ToString(),"Information",MessageBoxButtons.OK);
}
Extracting Substrings from Strings
● Line 19 uses the Substring method that takes one int argument. The
argument specifies the starting index from which the method copies
characters in the original string.
○ The substring returned contains a copy of the characters from the starting index to
the end of the string.
Demo
private void comboBox4_SelectedIndexChanged(object sender, EventArgs e)
char c = Convert.ToChar(textBox6.Text);
string s6 = textBox5.Text;
if (comboBox4.SelectedIndex == 0) { {
MessageBox.Show(s6.IndexOf(c).ToString()); }
if (comboBox4.SelectedIndex == 1) {
MessageBox.Show(s6.IndexOf(c, 1).ToString()); }
if (comboBox4.SelectedIndex == 3) {
if (comboBox4.SelectedIndex == 4) {
● ToLower()
○ Returns a new string with all the characters converted to lowercase
● Copy(string str)
○ Creates a new String object with the same value as the specified string.
10/29/2021 28
Cont.
● Replace(char oldChar, char newChar)
○ Replaces all occurrences of a specified Unicode character in the current string object with
the specified Unicode character and returns the new string.
● Replace(string oldValue, string newValue)
○ Replaces all occurrences of a specified string in the current string object with the specified
string and returns the new string.
● Trim()
○ Removes all leading and trailing white-space characters from the current String object
● Split(params char[] separator)
○ Returns a string array that contains the substrings in the current string object, delimited by
elements of a specified Unicode character array.
10/29/2021 29
Example
10/29/2021 30
StringBuilder
StringBuilder explained
● String class has many capabilities for processing strings. However a string’s contents can
never change – immutable.
● Eg. Concatenation of string (+=) - create new string and assigns its reference to the
variable.
● Class StringBuilder - used to create and manipulate dynamic string information—i.e.,
mutable (changeable).
● Every StringBuilder can store a certain number of characters that’s specified by its
capacity. Exceeding the capacity of a StringBuilder causes the capacity to expand to
accommodate the additional characters.
○ E.g concatenation method such as Append and AppendFormat – maintain without creating any
new string objects.
❖ StringBuilder is particularly useful for manipulating in place a large number of strings, as
it’s much more efficient than creating individual immutable strings.
StringBuilder Constructors
● Class StringBuilder provides six overloaded constructors.
○ E.g. var buffer1 = new StringBuilder(); // with default initial capacity
○ var buffer2 = new StringBuilder(10); //initial capacity spacified in int
○ var buffer3 = new StringBuilder("hello");// initialized with string content
● Output of:
○ Console.WriteLine($"buffer1 = \"{buffer1}\""); // buffer1 = “ “
Length and Capacity Properties, EnsureCapacity Method and Indexer of Class
StringBuilder
● Class StringBuilder provides overloaded Append methods that allow various types
of values to be added to the end of a StringBuilder.
● The Framework Class Library provides versions for each simple type and for
character arrays, strings and objects. (Remember that method ToString produces a
string representation of any object.)
● Each method takes an argument, converts it to a string and appends it to the
StringBuilder.
● object objectValue = "hello";
• // use method Append to append values to buffer
● var stringValue = "good bye";
● char[] characterArray = {'a', 'b', 'c', 'd', 'e', 'f'}; • buffer.Append(objectValue); buffer.Append(" ");
● var booleanValue = true; • buffer.Append(stringValue); buffer.Append(" ");
● var characterValue = 'Z';
• buffer.Append(characterArray); buffer.Append(" ");
● var integerValue = 7;
• buffer.Append(characterArray, 0, 3); buffer.Append("
● var longValue = 1000000L; // L suffix indicates a
");
long literal
● var floatValue = 2.5F; // F suffix indicates a float • buffer.Append(booleanValue); buffer.Append(" ");
literal • buffer.Append(characterValue); buffer.Append(" ");
● var doubleValue = 33.333;
• buffer.Append(integerValue); buffer.Append(" ");
● var buffer = new StringBuilder();
• buffer.Append(longValue); buffer.Append(" ");
• buffer.Append(doubleValue);
• Console.WriteLine($"buffer = {buffer.ToString()}");
Insert, Remove and Replace Methods of Class
StringBuilder
● Class StringBuilder also provides method Remove for deleting any portion of a
StringBuilder.
● Method Remove takes two arguments—the index at which to begin deletion
and the number of characters to delete.
● The sum of the starting index and the number of characters to be deleted
must always be less than the StringBuilder’s length; otherwise, the program
throws an ArgumentOutOfRangeException.
The Insert and Remove methods are demonstrated
• buffer.Insert(0, objectValue); buffer.Insert(0, " ");
● object objectValue = "hello"; • buffer.Insert(0, stringValue); buffer.Insert(0, " ");
● var stringValue = "good bye"; • buffer.Insert(0, characterArray); buffer.Insert(0, " ");
● char[] characterArray = {'a', 'b', 'c', 'd', 'e', 'f'};
• buffer.Insert(0, booleanValue); buffer.Insert(0, " ");
● var booleanValue = true;
● var characterValue = 'K'; • buffer.Insert(0, characterValue); buffer.Insert(0, " ");
● var integerValue = 7; • buffer.Insert(0, integerValue); buffer.Insert(0, " ");
● var longValue = 1000000L; // L suffix indicates a long literal
• buffer.Insert(0, longValue); buffer.Insert(0, " ");
● var floatValue = 2.5F; // F suffix indicates a float literal
● var doubleValue = 33.333; • buffer.Insert(0, floatValue); buffer.Insert(0, " ");
● var buffer = new StringBuilder(); • buffer.Insert(0, doubleValue); buffer.Insert(0, " ");
• Console.WriteLine($"buffer after Inserts: \n{buffer}\n");
• buffer.Remove(10, 1); // delete 2 in 2.5
• buffer.Remove(4, 4); // delete .333 in 33.333
• Console.WriteLine($"buffer after Removes:\n{buffer}");
● Another useful method included with StringBuilder is Replace, which searches for a
specified string or character and substitutes another string or character all occurrences.
○ var builder1 = new StringBuilder("Happy Birthday Jane");
○ var builder2 = new StringBuilder("goodbye greg");
○ Console.WriteLine($"Before replacements:\n{builder1}\n{builder2}");
○ builder1.Replace("Jane", "Greg");
○ builder2.Replace('g', 'G', 0, 5);//replace g by G if the char is found in the index specified
i.e. 0 - 5
○ Console.WriteLine($"\nAfter replacements:\n{builder1}\n{builder2}");
Regular Expressions
● Define Regular Expression(RegEx)
● Applications of RegEx
● How to design a RegEx expression
RegEx Explained
The .NET Framework provides several classes to help developers recognize and
manipulate regular expressions.
textBox1.Text = "ABC123fgh46koli";
label11.Text += s + "\n";
}
Elements of a RegEx
● Validate invoice number that has the following format the first three
are characters followed by 8 digits
READING ASSIGNMENT