Скачиваний:
64
Добавлен:
15.03.2015
Размер:
4.31 Mб
Скачать

516

Extreme C#

PART IV

The base class libraries include an extensive set of APIs for working with strings. This chapter goes beyond the string type and introduces specialized classes that make working with strings easier.

The String class is similar to the string type, but with much more power. Although the String class is very robust, it is also immutable, which means that once a String object is created, it can’t be modified.

When there is a need to manipulate strings, use the StringBuilder class. The StringBuilder class isn’t as streamlined as the String class, but it is built especially for modifying strings in any way necessary.

A topic related to strings is regular expressions. The String and StringBuilder classes have many capabilities, but regular expressions beat them both, hands down, at searching and text matching. The following sections go into detail about String and StringBuilder types, string formatting, and regular expressions.

Tip

The String and StringBuilder classes contain instance and static methods. When a String or StringBuilder instance already exists, use the instance method. However, you can gain performance advantages by avoiding unnecessary instantiations and using static methods.

The String Class

The String class mirrors the functionality of the string type, plus much more. There are numerous methods that compare, read, and search a String object’s contents.

Strings are immutable, meaning that they can’t be modified once created. All methods that appear to modify a String really don’t. They actually return a new String that has been modified based on the method invoked. When heavy modification is needed, use the StringBuilder class, which is discussed in the next section.

The String class is also sealed. This means that it can’t be inherited. Being immutable and sealed makes the String class more efficient. Now, let’s check out what the String class has to offer by examining its methods.

String Manipulation

CHAPTER 25

517

static Methods

The String class has several static methods. These are class methods that don’t need an instance of the String class to be invoked. The following paragraphs discuss the static String class methods.

The Compare() Method

The static Compare() method compares two strings, which are referred to here as str1 (string one) and str2 (string two). It produces the following integer results:

str1 < str2 = negative

str1 == str2 = zero

str1 > str2 = positive

An empty string, “”, is always greater than null. Here’s an example of how to implement the Compare() method:

int intResult;

string str1 = “string 1”; string str2 = “string 2”;

intResult = String.Compare(str1, str2);

Console.WriteLine(“String.Compare({0}, {1}) = {2}\n”, str1, str2, intResult);

The Compare() method has the following overloads:

Compare(str1, str2, ignoreCase)

Compare(str1, str2, ignoreCase, CultureInfo)

Compare(str1, index1, str2, index2, length)

Compare(str1, index1, str2, index2, length, ignoreCase)

Compare(str1, index1, str2, index2, length, ignoreCase, CultureInfo)

In these overloads, str1 and str2 are the strings to be compared, and index1 and index2 are the respective integer offsets into those strings to begin making the comparison. The length parameter is the number of characters to compare. The ignoreCase is a bool parameter where true means to ignore character case and false means to make a casesensitive comparison. CultureInfo is a class for specifying localization information.

25

TRINGS

ANIPULATIONM

518

Extreme C#

PART IV

The CompareOrdinal() Method

The static CompareOrdinal() method compares two strings—str1 (string one) and str2 (string two)—independent of localization. It produces the following integer results:

str1 < str2 = negative

str1 == str2 = zero

str1 > str2 = positive

An empty string, “”, is always greater than null. Here’s an example of how to implement the CompareOrdinal() method:

int intResult;

string str1 = “string 1”; string str2 = “string 2”;

intResult = String.CompareOrdinal(str2, str1);

Console.WriteLine(“String.CompareOrdinal({0}, {1}) = {2}\n”, str2, str1, intResult);

The CompareOrdinal() method has the following overload:

CompareOrdinal(str1, index1, str2, index2, length)

In this overload, str1 and str2 are the strings to be compared, and index1 and index2 are the respective integer offsets into those strings to begin making the comparison. The length parameter is the number of characters to compare.

The Concat() Method

The static Concat() method creates a new string from one or more input strings or objects. Here’s an example of how to implement the Concat() method using two strings:

int intResult;

string str1 = “string 1”; string str2 = “string 2”;

stringResult = String.Concat(str1, str2);

Console.WriteLine(“String.Concat({0}, {1}) = {2}\n”, str1, str2, stringResult);

The example shows the Concat() method accepting two string parameters. The result is a single string with the second string concatenated to the first. The Concat() method has the following overloads:

Concat (Object)

String Manipulation

CHAPTER 25

519

Concat (Object[])

Concat (String[])

Concat (Object, Object)

Concat (Object, Object, Object)

Concat (string, string, string)

Concat (string, string, string, string)

In these overloads, all object parameters are converted to String objects before concatenation. The elements of the array parameters are concatenated in order to create a single string. The other overloads, with multiple parameters, form a single String by concatenating each of the parameters in the order they appear.

The Copy() Method

The static Copy() method returns a copy of a String. Here’s an example of how to implement the Copy() method:

string stringResult; string str1 = “string 1”;

stringResult = String.Copy(str1);

Console.WriteLine(“String.Copy({0}) = {1}\n”, str1, stringResult);

The Copy() method makes a copy of str1. The result is a copy of str1 placed in

stringResult.

The Equals() Method

The static Equals() method determines whether two strings are equal, returning a bool value of true when they are equal and a bool value of false when they’re not. Here’s an example of how to implement the Equals() method:

bool boolResult;

string str1 = “string 1”; string str2 = “string 2”;

boolResult = String.Equals(str1, str2);

Console.WriteLine(“String.Equals({0}, {1}) = {2}\n”, str1, str2, boolResult);

The Equals() method accepts the two string parameters. The result is a bool that will evaluate to false because str1 and str2 are not the same value.

25

TRINGS

ANIPULATIONM

520

Extreme C#

PART IV

The Format() Method

The static Format() method returns a textual representation of an object after applying a specified format string. Here’s an example of how to implement the Format() method:

string stringResult; string str1 = “string 1”; string str2 = “string 2”;

String formatString = “{0,15}”;

stringResult = String.Format(formatString, str2);

Console.WriteLine(“String.Format({0}, {1}) = [{2}]\n”, formatString, str2, stringResult);

This example shows the Format() method accepting two string parameters. The first parameter is a format string that will be applied to the second parameter. The result is a 15-character string with the text right-aligned and padded to the left with spaces. The Format() method has the following overloads:

Format (string, Object[])

Format (IFormatProvider, string, Object[])

Format (string, Object, Object)

Format (string, Object, Object, Object)

In these overloads, the string parameter specifies the format string. Whether an array or individual object, each Object parameter is formatted according to its corresponding placeholder in the format string. The IFormatProvider is an interface that is implemented by an object for managing formatting.

The Intern() Method

The static Intern() method returns a reference to a string in the string intern pool. A C# program maintains a string intern pool where literal and constant strings are automatically placed. When strings are built on-the-fly (or programmatically), they are separate objects and are not intern pool members. The Intern() method will accept a parameter with a string that was programmatically constructed and return a reference to the identical string from the intern pool. Here’s an example of how to implement the Intern() method:

string

str1

= “string1”;

String

objStr1

= String.Concat(“string”, “1”);

String internedStr1 = String.Intern(objStr1);

String Manipulation

521

CHAPTER 25

 

Console.WriteLine(

 

“(object)objStr1 == (object)str1 is {0}\n”,

 

((object)objStr1 == (object)str1));

 

Console.WriteLine(

 

“(object)internedStr1 == (object)str1 is {0}\n”,

 

((object)internedStr1 == (object)str1));

 

The example shows the effects of using the Intern() method on a programmatically

 

constructed string. The Concat() method constructs a string on-the-fly, objStr1, that is

 

identical in value to str1. objStr1 is a separate object and not a member of the intern

 

string pool. The Intern() method returns a reference to a value in the intern pool that is

 

identical to the value of objStr1 (values are the same, but references are still different).

 

The first WriteLine() method will return the value false because objStr1 refers to a

 

separate object, and str1 refers to a literal string that was added to the intern pool. The

 

second WriteLine() method returns true because internedStr1 received a reference to

 

the intern pool, which is the same as str1 (references are the same).

 

The IsInterned() Method

 

The static IsInterned() method returns a reference to an interned string if it is a

 

member of the intern pool. Otherwise, it returns null. Here’s an example of how to

 

implement the IsInterned() method:

 

stringResult = String.IsInterned(internedStr1);

 

Console.WriteLine(“String.IsInterned({0}) = {1}\n”,

 

internedStr1, stringResult);

 

The example shows the IsInterned() method determining whether a string is in the

 

intern pool. Assuming that the internedStr1 string parameter has been interned, the

 

IsInterned() method will return a reference to that string in the intern pool.

 

Note

 

The intern pool is a system table that eliminates duplication by allowing multi-

 

ple references to the same constant string when the strings are identical. This

 

saves system memory. The intern-related methods of the string class enable a

 

program to determine if a string is interned and to place it in the intern pool to

25

take advantage of the associate memory optimizations.

TRINGS

ANIPULATIONM

522

Extreme C#

PART IV

The Join() Method

The static Join() method concatenates strings with a specified separator between them. Here’s an example of how to implement the Join() method:

string stringResult; string str1 = “string 1”; string str2 = “string 2”;

String[] strArr = new String[] { str1, str2 };

stringResult = String.Join(“,”, strArr);

Console.WriteLine(

“String.Join(\”,\”, [str1 and str2]) = {0}\n”, stringResult);

This example shows how to create a comma-separated list of strings with the Join() method. The first parameter of the Join() method specifies the separator character, a comma in this case. The second parameter is an array of strings that will be separated, resulting in a string where each member of the array is separated by the separation character. The Join() method has the following overload:

Join(string, stringArray, int1, int2)

In the preceding overload, string is the separator character and stringArray is an array of strings to be separated. The next two parameters are the beginning array element to start separating (that is, the first array element to be followed by the separation character) and the number of elements in the array to be separated.

Instance Methods

Instance String methods act upon an existing string object. Often referred to as this String, the instance acted upon by these methods is the same instance that is invoking the method.

The Clone() Method

The Clone() method returns this String. Here’s an example of how to implement the

Clone() method:

String stringResult; string str1 = “string 1”;

stringResult = (String)str1.Clone();

Console.WriteLine(“(String){0}.Clone() = {1}\n”, str1, stringResult);

String Manipulation

CHAPTER 25

523

The example demonstrates how the Clone() method returns a reference to the same instance it is invoked upon. Since the Clone() method returns an Object reference, the return value must be cast to a String before assignment to stringResult.

The CompareTo() Method

The CompareTo() method compares the value of this instance with a string. It produces the following integer results:

this < string = negative

this == string = zero

this > string = positive

string is null = 1

An empty string, “”, is always greater than null. If both this and string are null, then they are equal (zero result). Here’s an example of how to implement the CompareTo() method:

int intResult;

string str1 = “string 1”; string str2 = “string 2”;

intResult = str1.CompareTo(str2);

Console.WriteLine(“{0}.CompareTo({1}) = {2}\n”, str1, str2, intResult);

The CompareTo() method has the following overload:

CompareTo(Object)

In this overload, the Object parameter must be a String.

The CopyTo() Method

The CopyTo() method copies a specified number of characters from this String to an array of characters. Here’s an example of how to implement the CopyTo() method:

string str1 = “string 1”;

char[] charArr = new char[str1.Length];

str1.CopyTo(0, charArr, 0, str1.Length);

Console.WriteLine(

“{0}.CopyTo(0, charArr, 0, str1.Length) = “, str1);

25

TRINGS

ANIPULATIONM

524

Extreme C#

PART IV

foreach(char character in charArr)

{

Console.Write(“{0} “, character);

}

Console.WriteLine(“\n”);

This example shows the CopyTo() method filling a character array. It copies each character from str1 into charArr, beginning at position 0 and continuing for the length of str1. The foreach loop iterates through each element of charArr, printing the results.

The EndsWith() Method

The EndsWith() method determines if a String suffix matches a specified String. Here’s an example of how to implement the EndsWith() method:

bool boolResult;

string str1 = “string 1”; string str2 = “string 2”;

boolResult = str1.EndsWith(“2”);

Console.WriteLine(“{0}.EndsWith(\”2\”) = {1}\n”, str1, boolResult);

In this case, the EndsWith() method checks to see if str1 ends with the number 2. The result is false because str1 ends with the number 1.

The Equals() Method

The static Equals() method determines whether two strings are equal, returning a bool value of true when they are equal and a bool value of false when they’re not. Here’s an example of how to implement the Equals() method:

int intResult;

string str1 = “string 1”; string str2 = “string 2”;

boolResult = str1.Equals(str2);

Console.WriteLine(“{0}.Equals({1}) = {2}\n”, str1, str2, boolResult);

In this example the Equals() method accepts one string parameter. Since str1 has a different value than str2, the return value is false. The Equals() method has a single instance overload:

Compare(Object)

In the overload, the Object parameter must be a String.

String Manipulation

CHAPTER 25

The GetEnumerator() Method

The GetEnumerator() method returns a CharacterEnumerator for this String. The foreach statement uses IEnumerator to iterate through a collection. The CharacterEnumerator, returned by this method, is an IEnumerator. Here’s an example of how to implement the GetEnumerator() method:

string str1 = “string 1”;

CharEnumerator charEnum = str1.GetEnumerator();

Console.WriteLine(“charEnum is IEnumerator: {0}”, charEnum is IEnumerator);

The CharacterEnumerator inherits from the System.Collections.IEnumerator interface. This is why the value returned from the charEnum is IEnumerator expression in the Console.WriteLine() method will be true.

The IndexOf() Method

The IndexOf() method returns the position of a string or characters within this String. When the character or string is not found, IndexOf() returns –1. Here’s an example of how to implement the IndexOf() method:

int intResult;

string str1 = “string1”;

intResult = str1.IndexOf(‘1’);

Console.WriteLine(“str1.IndexOf(‘1’): {0}”, intResult);

The return value of this operation is 6 because that’s the zero-based position within str1 that the character ‘1’ occurs. The IndexOf() method has the following overloads:

IndexOf(char[])

IndexOf(string)

IndexOf(char, beginInt)

IndexOf(char[],beginInt)

IndexOf(string, beginInt)

IndexOf(char, beginInt, endInt)

IndexOf(char[],beginInt, endInt)

IndexOf(string, beginInt, endInt)

In these overloads, char[] parameters return the first instance of any character in the char array, and string parameters specify that the first instance of that string should be

525

25

TRINGS

ANIPULATIONM

526

Extreme C#

PART IV

searched for. The beginInt parameter means to start searching at that index within this String, and the endInt means to stop searching at that position within this String.

The Insert() Method

The Insert() method returns a string where a specified string is placed in a specified position of this String. All characters at and to the right of the insertion point are pushed right to make room for the inserted string. Here’s an example of how to implement the Insert() method:

string stringResult; string str2 = “string2”;

stringResult = str2.Insert(6, “1”);

Console.WriteLine(“str2.Insert(6, \”1\”): {0}”, stringResult);

This example places a “1” into str2, producing “string12”.

The LastIndexOf() Method

The LastIndexOf() method returns the position of the last occurrence of a string or characters within this String. Here’s an example of how to implement the

LastIndexOf() method:

int intResult;

string stateString = “Mississippi”;

intResult = stateString.LastIndexOf(‘s’);

Console.WriteLine(“stateString.LastIndexOf(‘s’): {0}”, intResult);

The preceding example shows how to use the LastIndexOf() method to find the position of the last occurrence of the letter ‘s’ in stateString. The zero-based result is 6. The LastIndexOf() method has the following overloads:

LastIndexOf(char[])

LastIndexOf(string)

LastIndexOf(char, beginInt)

LastIndexOf(char[],beginInt)

LastIndexOf(string, beginInt)

LastIndexOf(char, beginInt, endInt)

LastIndexOf(char[],beginInt, endInt)

LastIndexOf(string, beginInt, endInt)

String Manipulation

CHAPTER 25

527

In these overloads, char[] parameters return the last instance of any character in the char array, and string parameters specify that the last instance of that string should be searched for. The beginInt parameter means to start searching at that index within this String, and the endInt means to stop searching at that position within this String.

The PadLeft() Method

The PadLeft() method right aligns the characters of a string and pads on the left with spaces (by default) or a specified character. Here’s an example of how to implement the

Equals() method:

string stringResult; string str1 = “string 1”;

stringResult = str1.PadLeft(15);

Console.WriteLine(“str1.PadLeft(15): [{0}]”, stringResult);

In this example the PadLeft() method creates a 15-character string with the original string right aligned and filled to the left with space characters. The PadLeft() method has the following overload:

PadLeft(int, char)

It accepts an integer specifying the number of characters for the new string and a char parameter for the padding character.

The PadRight() Method

The PadRight() method left aligns the characters of a string and pads on the right with spaces (by default) or a specified character. Here’s an example of how to implement the

PadRight() method:

string stringResult; string str1 = “string 1”;

stringResult = str1.PadRight(15, ‘*’);

Console.WriteLine(“str1.PadRight(15, ‘*’): [{0}]”, stringResult);

The example shows the PadRight() method creating a 15-character string with the original string left aligned and filled to the right with ‘*’ characters. The PadRight() method has the following overload:

PadRight(int)

25

TRINGS

ANIPULATIONM

528

Extreme C#

PART IV

It accepts an integer specifying the number of characters for the new string, and it defaults to a space for the padding character.

The Remove() Method

The Remove() method deletes a specified number of characters from a position in this String. Here’s an example of how to implement the Remove() method:

string stringResult; string str2 = “string2”;

stringResult = str2.Remove(3, 3);

Console.WriteLine(“str2.Remove(3, 3): {0}”, stringResult);

This example shows the Remove() method deleting the fourth, fifth, and sixth characters from str2. The first parameter is the zero-based starting position to begin deleting, and the second parameter is the number of characters to delete. The result is “str2”, where the “ing” was removed from the original string.

The Replace() Method

The Replace() method replaces all occurrences of a character or string with a new character or string, respectively. Here’s an example of how to implement the Replace() method:

int intResult;

string str2 = “string 2”;

stringResult = str2.Replace(‘2’, ‘5’);

Console.WriteLine(“str2.Replace(‘2’, ‘5’): {0}”, stringResult);

In this example the Replace() method accepts two character parameters. The first parameter is the char to be replaced, and the second parameter is the char that will replace the first. The Replace() method has the following overload:

Replace(string, string)

In this overload, all occurrences of the first string are replaced by the second string.

The Split() Method

The Split() method extracts individual strings separated by a specified set of characters and places each of those strings into a string array. Here’s an example of how to implement the Split() method:

String Manipulation

CHAPTER 25

529

String csvString = “one, two, three”;

string[] stringArray = csvString.Split(new char[] {‘,’});

foreach( string strItem in stringArray )

{

Console.WriteLine(“Item: {0}”, strItem);

}

The example shows the Split() method extracting strings that are separated by commas. The individual strings “one”, “ two”, and “ three” are placed into a different index of stringArray. Notice the spaces before the strings “ two” and “ three”; that is how the Split() method preserves white space. The Split() method has the following overload:

Split(char[], int)

In this overload, char[] is an array of characters used as separators, and int is the number of strings to place into the resulting array.

The StartsWith() Method

The StartsWith() method determines if a String prefix matches a specified String. Here’s an example of how to implement the StartsWith() method:

bool boolResult;

string str1 = “string 1”;

boolResult = str1.StartsWith(“Str”);

Console.WriteLine(“str1.StartsWith(\”Str\”): {0}”, boolResult);

In this case, the StartsWith() method checks to see if str1 begins with the “Str”. The result is false because str1 begins with “str”, where the first character is lowercase.

The SubString() Method

The SubString() method retrieves a substring at a specified location from this String. Here’s an example of how to implement the SubString() method:

string stringResult; string str1 = “string1”;

stringResult = str1.Substring(3);

Console.WriteLine(“str1.Substring(3): {0}”, stringResult);

25

TRINGS

ANIPULATIONM

530

Extreme C#

PART IV

The result of this example is “ing1”. The SubString() method has the following overload:

SubString (int, int)

The first int is the zero-based position to begin extracting the substring from, and the second parameter is the number of characters to get.

The ToCharArray() Method

The ToCharArray() method copies the characters from this String into a character array. Here’s an example of how to implement the ToCharArray() method:

int intResult;

string str1 = “string1”;

char[] characterArray = str1.ToCharArray();

foreach( char character in charArr )

{

Console.WriteLine(“Char: {0}”, character);

}

The ToCharArray() method has the following overload:

ToCharArray(int, int)

The first int specifies the beginning of a substring to copy to the character array, and the second parameter indicates how many characters to move.

The ToLower() Method

The ToLower() method returns a copy of this string converted to lowercase characters. Here’s an example of how to implement the ToLower() method:

string stringResult;

string ucString = “UpperCaseString”;

stringResult = ucString.ToLower();

Console.WriteLine(“ucString.ToLower(): {0}”, stringResult);

The result of this example converts “UpperCaseString” to “uppercasestring”. The ToLower() method has the following overload:

ToLower(CultureInfo)

CultureInfo is a class for specifying localization information.

String Manipulation

CHAPTER 25

531

The ToUpper() Method

The ToUpper() method returns a copy of this String converted to uppercase characters. Here’s an example of how to implement the ToUpper() method:

string stringResult; string str1 = “string1”;

stringResult = str1.ToUpper();

Console.WriteLine(“str1.ToUpper(): {0}”, stringResult);

In this example, the result converts “string1” to “STRING1”. The ToUpper() method has the following overload:

ToUpper(CultureInfo)

CultureInfo is a class for specifying localization information.

The Trim() Method

The Trim() method removes whitespace or a specified set of characters from the beginning and ending of this String. Here’s an example of how to implement the Trim() method:

string stringResult;

string trimString = “ nonwhitespace “;

stringResult = trimString.Trim();

Console.WriteLine(“trimString.Trim(): [{0}]”, stringResult);

The example shows the Trim() method being used to remove all the whitespace from the beginning and end of trimString. The result is “nonwhitespace”, with no spaces on either side. The Trim() method has the following overload:

Trim(char[])

In this overload, char[] is an array of characters that are trimmed from the beginning and end of a string.

The TrimEnd() Method

The TrimEnd() method removes a specified set of characters from the end of this String. Here’s an example of how to implement the TrimEnd() method:

string stringResult;

string trimString = “ nonwhitespace “;

25

TRINGS

ANIPULATIONM

532

Extreme C#

PART IV

stringResult = trimString.TrimEnd(new char[] {‘ ‘});

Console.WriteLine(“trimString.TrimEnd(): [{0}]”, stringResult);

In this example the TrimEnd() method removes all the whitespace from the end of trimString. The result is “ nonwhitespace”, with no spaces on the right side.

The TrimStart() Method

The Trim() method removes whitespace or a specified set of characters from the beginning of this String. Here’s an example of how to implement the Trim() method:

string stringResult;

string trimString = “ nonwhitespace “;

stringResult = trimString.TrimStart(new char[] {‘ ‘});

Console.WriteLine(“trimString.TrimStart(): [{0}]”, stringResult);

Here, the TrimStart() method removes all the whitespace from the beginning of trimString. The result is “nonwhitespace”, with no spaces on the left side.

Properties and Indexers

The String class has a single property, Length, and an indexer.

The Length Property

The Length property returns the number of characters in a String. Here’s an example of how to implement the Length property:

int intResult;

string str1 = “string1”;

intResult = str1.Length;

Console.WriteLine(“str1.Length: {0}”, intResult);

The example shows the Length property being used to get the number of characters in str1. The result is 7.

The String Indexer

The String indexer returns a character within the string at a specified location. Here’s an example of how to implement the String indexer: