Lect None On Chapter 2 - Part II - String and StringBuilder
Lect None On Chapter 2 - Part II - String and StringBuilder
Regular Expressions
Overview
• The techniques in this section can be employed to
develop:
– text editors, word processors, page-layout software,
computerized typesetting systems and other kinds of text
processing software.
• Line 23 takes two int arguments. The first argument specifies the starting
index from which the method copies characters from the original string.
The second argument specifies the length of the substring to be copied.
The substring returned contains a copy of the specified characters from
the original string.
Concatenating string
• .net provide many ways to concatenate strings.
• The + operator:
– E.g. string name = “muna” ; name += “abay”;
• The static method Concat of class String concatenates two
strings and returns a new string containing the combined
characters from both original strings.
Miscellaneous String Methods
• Class String provides several methods that return modified copies of strings.
• The following demonstrates the use of String methods:
– Replace(), ToLower(), ToUpper(), Trim() and ToString().
• Line 27 uses String method Replace() to return a new string, replacing every
occurrence in string1 of character 'e' with character 'E'.
• Method Replace takes two arguments—a string for which to search and
another string with which to replace all matching occurrences of the first
argument. The original string remains unchanged. If there are no occurrences
of the first argument in the string, the method returns the original string.
• String method ToUpper generates a new string (line 31) that replaces any
lowercase letters in string1 with their uppercase equivalent.
• The method returns a new string containing the converted string; the original
string remains unchanged. If there are no characters to convert to uppercase, the
method returns the original string.
• Line 32 uses String method ToLower to return a new string in which any uppercase
letters in string1 are replaced by their lowercase equivalents. The original string is
unchanged. As with ToUpper, if there are no characters to convert to lowercase,
method ToLower returns the original string.
• Line 36 uses String method Trim to remove all whitespace characters that
appear at the beginning and end of a string. Without otherwise altering
the original string, the method returns a new string that contains the
string, but omits leading or trailing whitespace characters. Another version
of method Trim takes a character array and returns a string that does not
contain the characters in the array argument.
Class StringBuilder – namespace System.Text