The String Class


String is defined in the System namespace. It implements the IComparable, IComparable<string>, ICloneable, IEquatable<string>, IConvertible, IEnumerable and IEnumerable<string> interfaces. String is a sealed class, which means that it cannot be inherited. String provides string-handling functionality for C#. It underlies C#’s built-in string type and is part of the .NET Framework. The next few sections examine String in detail.

The String Constructors

The String class defines several constructors that allow you to construct a string in a variety of ways. To create a string from a character array, use one of these constructors:

 public String(char[ ] chrs) public String(char[ ] chrs, int start, int count)

The first form constructs a string that contains the characters in chrs. The second form uses count characters from chrs, beginning at the index specified by start.

You can create a string that contains a specific character repeated a number of times using this constructor:

 public String(char ch, int count)

Here, ch specifies the character that will be repeated count times.

You can construct a string given a pointer to a character array using one of these constructors:

 public String(char* chrs) public String(char* chrs, int start, int count)

The first form constructs a string that contains the characters pointed to by chrs. It is assumed that chrs points to a null-terminated array, which is used in its entirety. The second form uses count characters from the array pointed to by chrs, beginning at the index specified by start. Because they use pointers, these constructors can be used only in unsafe code.

You can construct a string given a pointer to an array of bytes using one of these constructors:

 public String(sbyte* chrs) public String(sbyte* chrs, int start, int count) public String(sbyte* chrs, int start, int count, Encoding en)

The first form constructs a string that contains the bytes pointed to by chrs. It is assumed that chrs points to a null-terminated array, which is used in its entirety. The second form uses count characters from the array pointed to by chrs, beginning at the index specified by start. The third form lets you specify how the bytes are encoded. The default encoding is ASCIIEncoding. The Encoding class is in the System.Text namespace. Because they use pointers, these constructors can be used only in unsafe code.

A string literal automatically creates a string object. For this reason, a string object is often initialized by assigning it a string literal, as shown here:

 string str = "a new string";

The String Field, Indexer, and Property

The String class defines one field, shown here:

 public static readonly string Empty

Empty specifies an empty string, which is a string that contains no characters. This differs from a null String reference, which simply refers to no object.

There is one read-only indexer defined for String, which is shown here:

 public char this[int idx] { get; }

This indexer allows you to obtain the character at a specified index. Like arrays, the indexing for strings begins at zero. Since String objects are immutable, it makes sense that String supports a read-only indexer.

There is one read-only property:

 public int Length { get; }

Length returns the number of characters in the string.

The String Operators

The String class overloads two operators: = = and !=. To test two strings for equality, use the = = operator. Normally, when the = = operator is applied to object references, it determines if both references refer to the same object. This differs for objects of type String. When the = = is applied to two String references, the contents of the strings, themselves, are compared for equality. The same is true for the != operator: When comparing String objects, the contents of the strings are compared. However, the other relational operators, such as < or >=, compare the references, just like they do for other types of objects. To determine if one string is greater than or less than another, use the Compare( ) method defined by String.

The String Methods

The String class defines a large number of methods, and many of the methods have two or more overloaded forms. For this reason it is neither practical nor useful to list them all. Instead, several of the more commonly used methods will be presented, along with examples that illustrate their use.

Comparing Strings

Perhaps the most frequently used string-handling operation is the comparison of one string to another. Because of its importance, String provides a wide array of comparison methods. These are shown in Table 21-1. Be aware that string comparisons are sensitive to cultural differences. Comparison methods that do not pass cultural information use the currently selected cultural settings.

Table 21-1: The String Comparison Methods

Method

Description

public static int

Compare(string str1, string str2)

Compares the string referred to by str1 with str2. Returns greater than zero if str1 is greater than str2, less than zero if str1 is less than str2, and zero if str1 and str2 are equal.

public static int

Compare(string str1, string str2, bool ignoreCase)

Compares the string referred to by str1 with str2. Returns greater than zero if str1 is greater than str2, less than zero if str1 is less than str2, and zero if str1 and str2 are equal. If ignoreCase is true, the comparison ignores case differences. Otherwise, case differences matter.

public static int

Compare(string str1, string str2, StringComparison how)

Compares the string referred to by str1 with str2. Returns greater than zero if str1 is greater than str2, less than zero if str1 is less than str2, and zero if str1 and str2 are equal. How the comparison is performed is specifi ed by how. (Added by C# 2.0.)

public static int

Compare(string str1, string str2, bool ignoreCase, CultureInfo ci)

Compares the string referred to by str1 with str2 using the cultural information passed in ci. Returns greater than zero if str1 is greater than str2, less than zero if str1 is less than str2, and zero if str1 and str2 are equal. If ignoreCase is true, the comparison ignores case differences. Otherwise, case differences matter. The CultureInfo class is defi ned in the System.Globalization namespace.

public static int

Compare(string str1, int start1, string str2, int start2, int count)

Compares portions of the strings referred to by str1 and str2. The comparison begins at str1[start1] and str2[start2] and runs for count characters. Returns greater than zero if str1 is greater than str2, less than zero if str1 is less than str2, and zero if str1 and str2 are equal.

public static int

Compare(string str1, int start1, string str2, int start2, int count, bool ignoreCase)

Compares portions of the strings referred to by str1 and str2. The comparison begins at str1[start1] and str2[start2] and runs for count characters. Returns greater than zero if str1 is greater than str2, less than zero if str1 is less than str2, and zero if str1 and str2 are equal. If ignoreCase is true, the comparison ignores case differences. Otherwise, case differences matter.

public static int

Compare(string str1, int start1, string str2, int start2, int count, StringComparison how)

Compares portions of the strings referred to by str1 and str2. The comparison begins at str1[start1] and str2[start2] and runs for count characters. Returns greater than zero if str1 is greater than str2, less than zero if str1 is less than str2, and zero if str1 and str2 are equal. How the comparison is performed is specifi ed by how. (Added by C# 2.0.)

public static int

Compare(string str1, int start1, string str2, int start2, int count, bool ignoreCase, CultureInfo ci)

Compares portions of the strings referred to by str1 and str2 using the cultural information passed in ci. The comparison begins at str1[start1] and str1[start2] and runs for count characters. Returns greater than zero if str1 is greater than str2, less than zero if str1 is less than str2, and zero if str1 and str2 are equal. If ignoreCase is true, the comparison ignores case differences. Otherwise, case differences matter. The CultureInfo class is defi ned in the System.Globalization namespace.

public static int

CompareOrdinal(string str1, string str2)

Compares the string referred to by str1 with str2 independently of culture, region, or language. Returns greater than zero if str1 is greater than str2, less than zero if str1 is less than str2, and zero if str1 and str2 are equal.

public static int

CompareOrdinal(string str1, int start1, string str2, int start2, int count)

Compares portions of the strings referred to by str1 and str2 independently of culture, region, or language. The comparison begins at str1[start1] and str2[start2] and runs for count characters. Returns greater than zero if str1 is greater than str2, less than zero if str1 is less than str2, and zero if str1 and str2 are equal.

public int CompareTo(object str)

Compares the invoking string with str. Returns greater than zero if the invoking string is greater than str, less than zero if the invoking string is less than str, and zero if the two are equal.

public int CompareTo(string str)

Compares the invoking string with str. Returns greater than zero if the invoking string is greater than str, less than zero if the invoking string is less than str, and zero if the two are equal.

Of the comparison methods, the Compare( ) method is the most versatile. It can compare two strings in their entirety or in parts. It can use case-sensitive comparisons or ignore case. In general, string comparisons use dictionary order to determine whether one string is greater than, equal to, or less than another. You can also specify cultural information that governs the comparison. The following program demonstrates several versions of Compare( ):

 // Compare strings. using System; class CompareDemo {   public static void Main() {     string str1 = "one";     string str2 = "one";     string str3 = "ONE";     string str4 = "two";     string str5 = "one, too";     if(String.Compare(str1, str2) == 0)       Console.WriteLine(str1 + " and " + str2 +                         " are equal.");     else       Console.WriteLine(str1 + " and " + str2 +                         " are not equal.");     if(String.Compare(str1, str3) == 0)       Console.WriteLine(str1 + " and " + str3 +                         " are equal.");     else       Console.WriteLine(str1 + " and " + str3 +                         " are not equal.");     if(String.Compare(str1, str3, true) == 0)       Console.WriteLine(str1 + " and " + str3 +                         " are equal ignoring case.");     else       Console.WriteLine(str1 + " and " + str3 +                         " are not equal ignoring case.");     if(String.Compare(str1, str5) == 0)       Console.WriteLine(str1 + " and " + str5 +                         " are equal.");     else       Console.WriteLine(str1 + " and " + str5 +                         " are not equal.");     if(String.Compare(str1, 0, str5, 0, 3) == 0)       Console.WriteLine("First part of " + str1 + " and " +                         str5 + " are equal.");     else       Console.WriteLine("First part of " + str1 + " and " +                         str5 + " are not equal.");     int result = String.Compare(str1, str4);     if(result < 0)       Console.WriteLine(str1 + " is less than " + str4);     else if(result > 0)       Console.WriteLine(str1 + " is greater than " + str4);     else       Console.WriteLine(str1 + " equals " + str4);   } }

The output is shown here:

 one and one are equal. one and ONE are not equal. one and ONE are equal ignoring case. one and one, too are not equal. First part of one and one, too are equal. one is less than two

Using the StringComparison Enumeration

In Table 21-1, notice that C# 2.0 added two new Compare( ) methods that take a parameter of type StringComparison. Their versions are shown here:

 public static int Compare(string str1, string str2, StringComparison how) public static int Compare(string str1, int start1, string str2, int start2,                          int count, StringComparison how)

For each version, the how parameter specifies how the comparison of str1 with str2 takes place. StringComparison is an enumeration that defines the values shown in Table 21-2. Using these values it is possible to craft a comparison that meets the specific needs of your application. Thus, the addition of the StringComparison parameter expands the capabilities of Compare( ).

Table 21-2: The StringComparison Enumeration Values

CurrentCulture

Comparisons are performed using the currently active cultural settings.

CurrentCultureIgnoreCase

Case-insensitive comparisons are performed using the currently active cultural settings.

InvariantCulture

Comparisons are performed using an invariant (that is, universal and unchanging) culture.

InvariantCultureIgnoreCase

Case-insensitive comparisons are performed using an invariant (that is, universal and unchanging) culture.

Ordinal

Comparisons are performed using the ordinal values of the characters in the string. Thus, dictionary order may not result and cultural conventions are ignored.

OrdinalIgnoreCase

Case-insensitive comparisons are performed using the ordinal values of the characters in the string. Thus, dictionary order may not result and cultural conventions are ignored.

One particularly good use of the StringComparison form of Compare( ) is to compare a string against an invariant filename. For example, imagine a situation in which the user must enter the password we~23&blx$. This password is the same no matter what cultural settings are in effect. Thus, you want to compare the string entered by the user to the password without cultural differences affecting the comparison. One way to do this is to specify StringComparison.InvariantCulture for the how parameter, in which case all cultural differences are avoided. This approach is demonstrated by the following program:

 // Compare strings using StringComparison enumeration. using System; class StrCompDemo {   public static void Main() {     string pswd = "we~23&blx$";     string str;     Console.WriteLine("Enter password: ");     str = Console.ReadLine();     // Compare using invariant culture.     if(String.Compare(pswd, str,                       StringComparison.InvariantCulture) == 0)       Console.WriteLine("Password accepted.");     else       Console.WriteLine("Password invalid.");   } }

Concatenating Strings

There are two ways to concatenate (join together) two or more strings. First, you can use the + operator, as demonstrated in Chapter 7. Second, you can use one of the various concatenation methods defined by String. Although using + is the easiest approach in many cases, the concatenation methods give you an alternative.

The method that performs concatenation is called Concat( ), and its most commonly used form is shown here:

 public static string Concat(string str1, string str2)

This method returns a string that contains str2 concatenated to the end of str1. Another form of Concat( ), shown here, concatenates three strings:

 public static string Concat(string str1, string str2, string str3)

In this version, a string that contains the concatenation of str1, str2, and str3 is returned. There is also a form that concatenates four strings:

 public static string Concat(string str1, string str2, string str3, string str4)

This version returns the concatenation of all four strings.

The version of Concat( ) shown next concatenates an arbitrary number of strings:

 public static string Concat(params string[ ] strs)

Here, strs refers to a variable number of arguments that are concatenated, and the result is returned. Because this version of concat( ) can be used to concatenate any number of strings, including two, three, or four strings, you might wonder why the other forms just shown exist. The reason is efficiency; passing up to four arguments is more efficient than using a variable-length argument list.

The following program demonstrates the variable-length argument version of Concat( ):

 // Demonstrate Concat(). using System; class ConcatDemo {   public static void Main() {     string result = String.Concat("This ", "is ", "a ",                                   "test ", "of ", "the ",                                   "String ", "class.");     Console.WriteLine("result: " + result);   } }

The output is shown here:

 result: This is a test of the String class.

There are also versions of the Concat( ) method that take object references, rather than string references. These obtain the string representation of the objects with which they are called and return a string containing the concatenation of those strings. (The string representations are obtained by calling ToString( ) on the objects.) These versions of Concat( ) are shown here:

 public static string Concat(object v1) public static string Concat(object v1, object v2) public static string Concat(object v1, object v2, object v3) public static string Concat(object v1, object v2, object v3, object v4) public static string Concat(params object[ ] v)

The first method simply returns the string equivalent of v1. The other methods return a string that contains the concatenation of their arguments. The object forms of Concat( ) are very convenient because they let you avoid having to manually obtain string representations prior to concatenation. To see how useful these methods can be, consider the following program:

 // Demonstrate the object form of Concat(). using System; class MyClass {   public static int count = 0;   public MyClass() { count++; } } class ConcatDemo {   public static void Main() {     string result = String.Concat("The value is " + 19);     Console.WriteLine("result: " + result);     result = String.Concat("hello ", 88, " ", 20.0, " ",                            false, " ",  23.45M);     Console.WriteLine("result: " + result);     MyClass mc = new MyClass();     result = String.Concat(mc, " current count is ",                            MyClass.count);     Console.WriteLine("result: " + result);   } }

The output is shown here:

 result: The value is 19 result: hello 88 20 False 23.45 result: MyClass current count is 1

In this example, Concat( ) concatenates the string representations of various types of data. For each argument, the ToString( ) method associated with that argument is called to obtain a string representation. Thus, in this call to Concat( ):

 string result = String.Concat("The value is " + 19);

Int32.ToString( ) is invoked to obtain the string representation of the integer value 19. Concat( ) then concatenates the strings and returns the result.

Also notice how an object of the user-defined class MyClass can be used in this call to Concat( ):

 result = String.Concat(mc, " current count is ",                        MyClass.count);   

In this case, the string representation of mc, which is of type MyClass, is returned. By default, this is simply its class name. However, if you override the ToString( ) method, then MyClass can return a different string. For example, try adding this version of ToString( ) to MyClass in the preceding program:

 public override string ToString() {   return "An object of type MyClass"; }

When this version is used, the last line in the output will be

 result: An object of type MyClass current count is 1

Searching a String

String offers many methods that allow you to search a string. For example, you can search for either a substring or a character. You can also search for the first or last occurrence of either.

To search for the first occurrence of a character or substring, use the IndexOf( ) method. Here are two of its forms:

 public int IndexOf(char ch) public int IndexOf(String str)

The first form returns the index of the first occurrence of the character ch within the invoking string. The second form returns the first occurrence of the string str. Both return 1 if the item is not found.

To search for the last occurrence of a character or substring, use the LastIndexOf( ) method. Here are two of its forms:

 public int LastIndexOf(char ch) public int LastIndexOf(string str)

The first form returns the index of the last occurrence of the character ch within the invoking string. The second form returns the index of the last occurrence of the string str. Both return 1 if the item is not found.

String offers two interesting supplemental search methods: IndexOfAny( ) and LastIndexOfAny( ). These search for the first or last character that matches any of a set of characters. Here are their simplest forms:

 public int IndexOfAny(char[ ] a) public int LastIndexOfAny(char[ ] a)

IndexOfAny( ) returns the index of the first occurrence of any character in a that is found within the invoking string. LastIndexOfAny( ) returns the index of the last occurrence of any character in a that is found within the invoking string. Both return 1 if no match is found.

When working with strings, it is often useful to know if a string begins with or ends with a given substring. To accomplish these tasks, use the StartsWith( ) and EndsWith( ) methods. Here are their two simplest forms:

 public bool StartsWith(string str) public bool EndsWith(string str)

StartsWith( ) returns true if the invoking string begins with the string passed in str. EndsWith( ) returns true if the invoking string ends with the string passed in str. Both return false on failure.

Here is a program that demonstrates several of the string search methods:

 // Search strings. using System; class StringSearchDemo {   public static void Main() {     string str = "C# has powerful string handling.";     int idx;     Console.WriteLine("str: " + str);     idx = str.IndexOf('h');     Console.WriteLine("Index of first 'h': " + idx);     idx = str.LastIndexOf('h');     Console.WriteLine("Index of last 'h': " + idx);     idx = str.IndexOf("ing");     Console.WriteLine("Index of first \"ing\": " + idx);     idx = str.LastIndexOf("ing");     Console.WriteLine("Index of last \"ing\": " + idx);     char[] chrs = { 'a', 'b', 'c' };     idx = str.IndexOfAny(chrs);     Console.WriteLine("Index of first 'a', 'b', or 'c': " + idx);     if(str.StartsWith("C# has"))       Console.WriteLine("str begins with \"C# has\"");     if(str.EndsWith("ling."))       Console.WriteLine("str ends with \"ling.\"");   } }

The output from the program is shown here:

 str: C# has powerful string handling. Index of first 'h': 3 Index of last 'h': 23 Index of first "ing": 19 Index of last "ing": 28 Index of first 'a', 'b', or 'c': 4 str begins with "C# has" str ends with "ling."

C# 2.0 adds a new string search method that you will find useful in many circumstances: Contains( ). Its general form is shown here:

 public bool Contains(string str)

It returns true if the invoking string contains the string specified by str, and false otherwise. This method is especially useful when all you need to know is if a specific substring exists within another string. Here is an example that demonstrates its use:

 // Demonstrate Contains(). using System; class ContainsDemo {   public static void Main() {     string str = "C# combines power with performance.";     if(str.Contains("power"))       Console.WriteLine("The sequence power was found.");     if(str.Contains("pow"))       Console.WriteLine("The sequence pow was found.");     if(!str.Contains("powerful"))       Console.WriteLine("The sequence powerful was not found.");   } }

The output is shown here:

 The sequence power was found. The sequence pow was found. The sequence powerful was not found.

As the output shows, Contains( ) searches for a matching sequence, not for whole words. Thus, both “pow” and “power” are found. However, since there is no sequence that matches “powerful”, it is (correctly) not found.

Several of the search methods have additional forms that allow you to begin a search at a specified index or to specify a range to search within. All versions of the String search methods are shown in Table 21-3.

Table 21-3: The Search Methods Offered by String

Method

Description

public bool Contains(string str)

Returns true if the invoking string contains the string specified by str. false is returned if str is not found. (Added by C# 2.0.)

public bool EndsWith(string str)

Returns true if the invoking string ends with the string passed in str. Otherwise, false is returned.

public bool EndsWith(string str, StringComparison how)

Returns true if the invoking string ends with the string passed in str. Otherwise, false is returned. How the search is performed is specified by how. (Added by C# 2.0.)

public bool EndsWith(string str, bool ignoreCase, CultureInfo ci)

Returns true if the invoking string ends with the string passed in str. Otherwise, false is returned. If ignoreCase is true, the search ignores case differences. Otherwise, case differences matter. The search is conducted using the cultural information passed in ci. (Added by C# 2.0.)

public int IndexOf(char ch)

Returns the index of the first occurrence of ch within the invoking string. Returns 1 if ch is not found.

public int IndexOf(string str)

Returns the index of the first occurrence of str within the invoking string. Returns 1 if str is not found.

public int IndexOf(char ch, int start)

Returns the index of the first occurrence of ch within the invoking string. Searching begins at the index specified by start. Returns 1 if ch is not found.

public int IndexOf(string str, int start)

Returns the index of the first occurrence of str within the invoking string. Searching begins at the index specified by start. Returns 1 if str is not found.

public int IndexOf(char ch, int start, int count)

Returns the index of the first occurrence of ch within the invoking string. Searching begins at the index specified by start and runs for count elements. Returns 1 if ch is not found.

public int IndexOf(string str, int start, int count)

Returns the index of the first occurrence of str within the invoking string. Searching begins at the index specified by start and runs for count elements. Returns 1 if str is not found.

public int IndexOf(string str, StringComparison how)

Returns the index of the first occurrence of str within the invoking string. How the search is performed is specified by how. Returns 1 if str is not found. (Added by C# 2.0.)

public int IndexOf(string str, int start, StringComparison how)

Returns the index of the first occurrence of str within the invoking string. Searching begins at the index specified by start. How the search is performed is specified by how. Returns 1 if str is not found. (Added by C# 2.0.)

public int IndexOf(string str, int start, int count, StringComparison how)

Returns the index of the first occurrence of str within the invoking string. Searching begins at the index specified by start and runs for count elements. How the search is performed is specified by how. Returns 1 if ch is not found. (Added by C# 2.0.)

public int IndexOfAny(char[ ] a)

Returns the index of the first occurrence of any character in a that is found within the invoking string. Returns 1 if no match is found.

public int IndexOfAny(char[ ] a, int start)

Returns the index of the first occurrence of any character in a that is found within the invoking string. Searching begins at the index specified by start. Returns 1 if no match is found.

public int IndexOfAny(char[ ] a, int start, int count)

Returns the index of the first occurrence of any character in a that is found within the invoking string. Searching begins at the index specified by start and runs for count elements. Returns 1 if no match is found.

public int LastIndexOf(char ch)

Returns the index of the last occurrence of ch within the invoking string. Returns 1 if ch is not found.

public int LastIndexOf(string str)

Returns the index of the last occurrence of str within the invoking string. Returns 1 if str is not found.

public int LastIndexOf(char ch, int start)

Returns the index of the last occurrence of ch within a range of the invoking string. The search proceeds in reverse order, beginning at the index specified by start and stopping at zero. Returns 1 if the ch is not found.

public int LastIndexOf(string str, int start)

Returns the index of the last occurrence of str within a range of the invoking string. The search proceeds in reverse order, beginning at the index specified by start and stopping at zero. Returns 1 if str is not found.

public int LastIndexOf(char ch, int start, int count)

Returns the index of the last occurrence of ch within the invoking string. The search proceeds in reverse order, beginning at the index specified by start and running for count elements. Returns 1 if ch is not found.

public int LastIndexOf(string str, int start, int count)

Returns the index of the last occurrence of str within the invoking string. The search proceeds in reverse order, beginning at the index specified by start and running for count elements. Returns 1 if str is not found.

public int LastIndexOf(string str, StringComparison how)

Returns the index of the last occurrence of str within the invoking string. How the search is performed is specified by how. Returns 1 if str is not found. (Added by C# 2.0.)

public int LastIndexOf(string str, int start, StringComparison how)

Returns the index of the last occurrence of str within a range of the invoking string. The search proceeds in reverse order, beginning at the index specified by start and stopping at zero. How the search is performed is specified by how. Returns 1 if str is not found. (Added by C# 2.0.)

public int LastIndexOf(string str, int start, int count, StringComparison how)

Returns the index of the last occurrence of str within the invoking string. The search proceeds in reverse order, beginning at the index specified by start and running for count elements. How the search is performed is specified by how. Returns 1 if str is not found. (Added by C# 2.0.)

public int LastIndexOfAny(char[ ] a)

Returns the index of the last occurrence of any character in a that is found within the invoking string. Returns 1 if no match is found.

public int LastIndexOfAny(char[ ] a, int start)

Returns the index of the last occurrence of any character in a that is found within the invoking string. The search proceeds in reverse order, beginning at the index specified by start and stopping at zero. Returns 1 if no match is found.

public int LastIndexOfAny(char[ ] a, int start, int count)

Returns the index of the last occurrence of any character in a that is found within the invoking string. The search proceeds in reverse order, beginning at the index specified by start and running for count elements. Returns 1 if no match is found.

public bool StartsWith(string str)

Returns true if the invoking string begins with the string passed in str. Otherwise, false is returned.

public bool StartsWith(string str, StringComparison how)

Returns true if the invoking string begins with the string passed in str. Otherwise, false is returned. How the search is performed is specified by how. (Added by C# 2.0.)

public bool StartsWith(string str, bool ignoreCase, CultureInfo ci)

Returns true if the invoking string begins with the string passed in str. Otherwise, false is returned. If ignoreCase is true, the search ignores case differences. Otherwise, case differences matter. The search is conducted using the cultural information passed in ci. (Added by C# 2.0.)

Splitting and Joining Strings

Two fundamental string-handling operations are split and join. A split decomposes a string into its constituent parts. A join constructs a string from a set of parts. To split a string, String defines Split( ). To join a set of strings, String provides Join( ).

There are several versions of Split( ). Two commonly used forms, which have been available since C# 1.0, are shown here:

 public string[ ] Split(params char[ ] seps) public string[ ] Split(char[ ] seps, int count)

The first form splits the invoking string into pieces and returns an array containing the substrings. The characters that delimit each substring are passed in seps. If seps is null, then whitespace is used as the separator. In the second form, no more than count substrings will be returned.

The two forms of the Join( ) method are shown here:

 public static string Join(string sep, string[ ] strs) public static string Join(string sep, string[ ] strs, int start, int count)

The first form returns a string that contains the concatenation of the strings in strs. The second form returns a string that contains the concatenation of count strings in strs, beginning at strs[start]. For both versions, each string is separated from the next by the string specified by sep.

The following program demonstrates Split( ) and Join( ):

 // Split and join strings. using System; class SplitAndJoinDemo {   public static void Main() {     string str = "One if by land, two if by sea.";     char[] seps = {' ', '.', ',' };     // Split the string into parts.     string[] parts = str.Split(seps);     Console.WriteLine("Pieces from split: ");     for(int i=0; i < parts.Length; i++)       Console.WriteLine(parts[i]);     // Now, join the parts.     string whole = String.Join(" | ", parts);     Console.WriteLine("Result of join: ");     Console.WriteLine(whole);   } }

Here is the output:

 Pieces from split: One if by land two if by sea Result of join: One | if | by | land |  | two | if | by | sea |

There is one important thing to notice in this output: the empty string that occurs between “land” and “two”. This is caused by the fact that in the original string, the word “land” is followed by a comma and a space, as in “land, two”. However, both the comma and the space are specified as separators. Thus, when this string is split, the empty string that exists between the two separators (the comma and the space) is returned.

C# 2.0 added several additional forms of Split( ) that take a parameter of type StringSplitOptions. This parameter controls whether empty strings are part of the resulting split. Here are these new forms of Split( ):

 public string[ ] Split(char[ ] seps, StringSplitOptions how) public string[ ] Split(string[ ] seps, StringSplitOptions how) public string[ ] Split(char[ ] seps, int count, StringSplitOptions how) public string[ ] Split(string[ ] seps, int count, StringSplitOptions how)

The first two forms split the invoking string into pieces and return an array containing the substrings. The characters that delimit each substring are passed in seps. If seps is null, then whitespace is used as the separator. In the third and fourth forms, no more than count substrings will be returned. For all versions, the value of how determines how to handle empty strings that result when two separators are adjacent to each other. The StringSplitOptions enumeration defines only two values: None and RemoveEmptyEntries. If how is None, then empty strings are included in the result (as the previous program showed). If how is RemoveEmptyEntries, empty strings are excluded from the result.

To understand the effects of removing empty entries, try replacing this line in the preceding program:

 string[] parts = str.Split(seps);

with the following:

 string[] parts = str.Split(seps, StringSplitOptions.RemoveEmptyEntries);

When you run the program, the output will be as shown next:

 Pieces from split: One if by land two if by sea Result of join: One | if | by | land | two | if | by | sea

As you can see, the empty string that previously resulted because of the combination of the comma and space after “land” has been removed.

Splitting a string is an important string-manipulation procedure, because it is often used to obtain the individual tokens that comprise the string. For example, a database program might use Split( ) to decompose a query such as “show me all balances greater than 100” into its individual parts, such as “show” and “100”. In the process, the separators are removed. Thus, “show” (without any leading or trailing spaces) is obtained, not “ show ”. The following program illustrates this concept. It tokenizes strings containing binary mathematical operations, such as 10 + 5. It then performs the operation and displays the result.

 // Tokenize strings. using System; class TokenizeDemo {   public static void Main() {     string[] input = {                       "100 + 19",                       "100 / 3.3",                       "-3 * 9",                       "100 - 87"                      };     char[] seps = {' '};     for(int i=0; i < input.Length; i++) {       // split string into parts       string[] parts = input[i].Split(seps);       Console.Write("Command: ");       for(int j=0; j < parts.Length; j++)         Console.Write(parts[j] + " ");       Console.Write(", Result: ");       double n = Double.Parse(parts[0]);       double n2 = Double.Parse(parts[2]);       switch(parts[1]) {         case "+":           Console.WriteLine(n + n2);           break;         case "-":           Console.WriteLine(n - n2);           break;         case "*":           Console.WriteLine(n * n2);           break;         case "/":           Console.WriteLine(n / n2);           break;       }     }   } } 

Here is the output:

 Command: 100 + 19 , Result: 119 Command: 100 / 3.3 , Result: 30.3030303030303 Command: -3 * 9 , Result: -27 Command: 100 - 87 , Result: 13

Padding and Trimming Strings

Sometimes you will want to remove leading and trailing spaces from a string. This type of operation, called trimming, is often needed by command processors. For example, a database might recognize the word “print”. However, a user might enter this command with one or more leading or trailing spaces. Any such spaces must be removed before the string can be recognized by the database. Conversely, sometimes you will want to pad a string with spaces so that it meets some minimal length. For example, if you are preparing formatted output, you might need to ensure that each line is of a certain length in order to maintain an alignment. Fortunately, C# includes methods that make these types of operations easy.

To trim a string, use one of these Trim( ) methods:

 public string Trim( ) public string Trim(params char[ ] chrs)

The first form removes leading and trailing whitespace from the invoking string. The second form removes leading and trailing occurrences of the characters specified by chrs. In both cases the resulting string is returned.

You can pad a string by adding characters to either the left or the right side of the string. To pad a string on the left, use one of the methods shown here:

 public string PadLeft(int len) public string PadLeft(int len, char ch)

The first form adds spaces on the left as needed to the invoking string so that its total length equals len. The second form adds the characters specified by ch as needed to the invoking string so that its total length equals len. In both cases, the resulting string is returned.

To pad a string to the right, use one of these methods:

 public string PadRight(int len) public string PadRight(int len, char ch)

The first form adds spaces on the right as needed to the invoking string so that its total length equals len. The second form adds the characters specified by ch as needed to the invoking string so that its total length equals len. In both cases, the resulting string is returned.

The following program demonstrates trimming and padding:

 // Trimming and padding. using System; class TrimPadDemo {   public static void Main() {     string str = "test";     Console.WriteLine("Original string: " + str);     // Pad on left with spaces.     str = str.PadLeft(10);     Console.WriteLine("|" + str + "|");     // Pad on right with spaces.     str = str.PadRight(20);     Console.WriteLine("|" + str + "|");     // Trim spaces.     str = str.Trim();     Console.WriteLine("|" + str + "|");     // Pad on left with #s.     str = str.PadLeft(10, '#');     Console.WriteLine("|" + str + "|");     // Pad on right with #s.     str = str.PadRight(20, '#');     Console.WriteLine("|" + str + "|");     // Trim #s.     str = str.Trim('#');     Console.WriteLine("|" + str + "|");   } }

The output is shown here:

 Original string: test |      test| |      test          | |test| |######test| |######test##########| |test|

Inserting, Removing, and Replacing

You can insert a string into another using the Insert( ) method, shown here:

 public string Insert(int start, string str)

Here, str is inserted into the invoking string at the index specified by start. The resulting string is returned.

You can remove a portion of a string using Remove( ), shown next:

 public string Remove(int start) public string Remove(int start, int count)

The first form begins at the index specified by start and removes all remaining characters in the string. The second form begins at count and removes count number of characters. In both cases, the resulting string is returned. One other point: the first form was added by C# 2.0.

You can replace a portion of a string by using Replace( ). It has these forms:

 public string Replace(char ch1, char ch2) public string Replace(string str1, string str2)

The first form replaces all occurrences of ch1 in the invoking string with ch2. The second form replaces all occurrences of str1 in the invoking string with str2. In both cases, the resulting string is returned.

Here is an example that demonstrates Insert( ), Remove( ), and Replace( ):

 // Inserting, replacing, and removing. using System; class InsRepRevDemo {   public static void Main() {     string str = "This test";     Console.WriteLine("Original string: " + str);     // Insert     str = str.Insert(5, "is a ");     Console.WriteLine(str);     // Replace string     str = str.Replace("is", "was");     Console.WriteLine(str);     // Replace characters     str = str.Replace('a', 'X');     Console.WriteLine(str);     // Remove     str = str.Remove(4, 5);     Console.WriteLine(str);   } }

The output is shown here:

 Original string: This test This is a test Thwas was a test ThwXs wXs X test ThwX X test

Changing Case

String offers two convenient methods that enable you to change the case of letters within a string. These are called ToUpper( ) and ToLower( ), and they are shown here:

 public string ToLower( ) public string ToUpper( )

ToLower( ) lowercases all letters within the invoking string. ToUpper( ) uppercases all letters within the invoking string. The resulting string is returned. There are also versions of these methods that allow you to specify cultural settings.

C# 2.0 added the methods ToUpperInvariant( ) and ToLowerInvariant( ), shown here:

 public string ToUpperInvariant( ) public string ToLowerInvariant( )

These work like ToUpper( ) and ToLower( ) except that they use the invariant culture to perform the transformations to upper- or lowercase.

Using the Substring( ) Method

You can obtain a portion of a string by using the Substring( ) method. It has these two forms:

 public string Substring(int idx) public string Substring(int idx, int count)

In the first form, the substring begins at the index specified by idx and runs to the end of the invoking string. In the second form, the substring begins at idx and runs for count characters. In each case the substring is returned.

The following program demonstrates the Substring( ) method:

 // Use Substring(). using System; class SubstringDemo {   public static void Main() {     string str = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";     Console.WriteLine("str: " + str);     Console.Write("str.Substring(15): ");     string substr = str.Substring(15);     Console.WriteLine(substr);     Console.Write("str.Substring(0, 15): ");     substr = str.Substring(0, 15);     Console.WriteLine(substr);   } }

The following output is produced:

 str: ABCDEFGHIJKLMNOPQRSTUVWXYZ str.Substring(15): PQRSTUVWXYZ str.Substring(0, 15): ABCDEFGHIJKLMNO




C# 2.0(c) The Complete Reference
C# 2.0: The Complete Reference (Complete Reference Series)
ISBN: 0072262095
EAN: 2147483647
Year: 2006
Pages: 300

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net