#105 – Chaining String Functions Together

Because many functions that operate on strings return a modified string, you can then pass that resulting string into another function that will operate on the new string.  In this way, you can chain together several string operations on the same line of code.

 char[] braces = new char[] {'{', '}'};
 string s = "{This|That|Such}";
 s = s.Replace("|", " and ").Trim(braces).Insert(0, "=> ").ToLower();
 Console.WriteLine(s);       // => this and that and such
Advertisement

#104 – Functions to Trim Leading and Trailing Characters from A String

You can use the Trim function to trim leading and trailing whitespace characters from a string.

 string s = "  The core phrase ";  // 2 leading spaces, 1 trailing
 s = s.Trim();     // s = "The core phrase"

Once again, the function does not change the original string, so you have to assign the result to some string.

You can also give Trim a list of characters that you want trimmed:

 string s = "  {The core phrase,} ";
 s = s.Trim(new char[] {' ','{',',','}'});     // s = "The core phrase"
 s = " {Doesn't {trim} internal stuff }";
 s = s.Trim(new char[] {' ', '{', '}'});      // s = "Doesn't {trim} internal stuff"

Finally, you can trim stuff from the start or end of the string with TrimStart and TrimEnd.

 string s = "{Name}";
 char[] braces = new char[] {'{', '}'};
 string s2 = s.TrimStart(braces);    // s2 = "Name}"
 s2 = s.TrimEnd(braces);             // s2 = "{Name"

#103 – Inserting and Removing Substrings

You can use the String.Insert method to insert a substring into the middle of an existing string.

 string s = "John Adams";
 int n = s.IndexOf("Adams");
 s = s.Insert(n, "Quincy ");    // s now "John Quincy Adams"

Remember that a string is immutable.  Invoking Insert on a string but not assigning the result to anything will have no effect on the string.

 string s = "John Adams";
 s.Insert(5, "Quincy ");    // Allowed, but s is not changed

You can use the String.Remove method to remove a specified number of characters from a string, starting at specified 0-based index.  (Again, you must assign the resulting string to something).

 string s = "OHOLEne";
 s = s.Remove(1, 4);      // Start at position 1, remove 4 characters
 Console.WriteLine(s);    // "One"

#102 – Use Substring() to Extract Substrings From A String

The String.IndexOf method searched for the location of a substring in a larger string, given the substring.  You can use the Substring method to do the reverse–extract a substring, given its position in the larger string.

 string s = "Itsy bitsy spider";
 string s2 = s.Substring(5);  // bitsy spider  (start at 6th character, extract until end of string)
 s2 = s.Substring(5, 4);      // bits
 s.Substring(2, s.Length);    // throws ArgumentOutOfRangeException

Also note that the Substring method doesn’t change the original string, but just returns the substring asked for.

#101 – Use Contains() to Discover If A String Contains Other Strings

You can use String.IndexOf to search for a substring within another stringIndexOf returns a 0-based index of the string that you’re searching for.

But if you don’t care about the location of the substring and just want to know whether it’s contained in the larger string, you can use the String.Contains method.  You can also search for individual characters.  Note that the search is case sensitive.

 string s = "A man, a plan, a canal";
 bool b = s.Contains("canal");   // true
 b = s.Contains("Canal");        // false (case-sensitive)
 b = s.Contains('p');            // true

You can also search for a particular substring at the start or the end of a string.

string s = "Call me Ishmael";
 bool b = s.StartsWith("me");       // false
 b = s.EndsWith("Ishmael");         // true
 b = s.StartsWith("Call");          // true

#100 – Using IndexOf to Search for Characters Within A String

You can search for the following within a string:

  • A single character
  • One of a set of characters
  • A substring

You search for a single character in a string using the String.IndexOf method, which returns a 0-based index into the string.

 string s = "Thomas Paine";
 int n = s.IndexOf('a');     // 4 (1st 'a')

You can also specify a starting position for the search.  So we can find the next occurrence of ‘a’ this way:

 n = s.IndexOf('a', n+1);    // 8 (start search after 1st 'a')

You can search for the first occurrence of one of a set of characters using the IndexOfAny method.

 string s = "Thomas Paine";
 char[] vowels = new char[] {'a','e','i','o','u'};
 int n = s.IndexOfAny(vowels);     // 2
 n = s.IndexOfAny(vowels, n + 1);  // 4

You can also use IndexOf to search for substrings within a string.

 string s = "A man, a plan, a canal";
 int n = s.IndexOf("an");       // 3
 n = s.IndexOf("an", n + 1);    // 11

#99 – Use StringInfo to Get Specific Characters From A UTF32 String

We saw that you cannot use the normal string index [] to get individual characters from a UTF32 string.  Instead, you need to use the System.Globalization.StringInfo class.

In the example below, we first get a list of indexes to each of the three characters in our UTF32 string.  We then extract index each character separately.

 s = "A𠈓C";
 int n = s.Length;     // 4, because of 4-byte character in middle

 // Get locations of text elements
 int[] indexes = StringInfo.ParseCombiningCharacters(s);  // 0, 1 and 3

 // Retrieve single element
 string nextChar = StringInfo.GetNextTextElement(s, 0);   // A
 nextChar = StringInfo.GetNextTextElement(s, 1);          // 𠈓
 nextChar = StringInfo.GetNextTextElement(s, 3);          // C

#98 – Using an Indexer to Get a Specific Character In a String

In C#, you can use the indexer operator [ ], to get a specified character in a string.

The indexer takes a zero-based integer as an index into the string.  0 returns the first character and n-1 (where n is the length of the string) returns the last character.

 string s = "ABCDE";
 char c = s[0];   // A
 c = s[2];        // C (3rd char)
 c = s[4];        // E

Using a negative value for the index will result in an IndexOutOfRangeException being thrown.

Note that indexers work to extract Unicode characters only if they are 2-byte UTF16 characters.  The indexer cannot retrieve a 4-byte surrogate pair.

 string s = "A€C";
 char c = s[1];         // Works: €

 s = "A𠈓C";
 c = s[1];       // Doesn't work: unprintable character

#97 – String Comparisons Using Other Cultures

Each language has different rules for how to sort strings, based on the alphabetical ordering of the characters used for that language.  In .NET, information about a language/place combination is captured in an instance of the CultureInfo class.  In turn, the CultureInfo object has a CompareInfo property that points to an instance of the CompareInfo class, defining rules for sorting strings in that culture.

By default, when you use the String.Compare method, the sorting rules for the current culture (CultureInfo.CurrentCulture) are used.  But you can override this by adding a CultureInfo parameter on the Compare method.

You can pass a new instance of CultureInfo by creating an instance using a unique culture code.  The culture code indicates language and region.

 int n;
 n = string.Compare("åb", "bb", true);  // -1 (å < b in English)
 n = string.Compare("åb", "bb", true, new CultureInfo("nn-NO"));    // 1  (å > b in Norwegian)

#96 – Comparing String Values

The < and > operators are not overloaded for the System.String type, which means that you can’t compare strings using the relational operators.

Instead. you can use the static System.String.Compare method, which takes two strings and returns an integer value.  If the first string is less than the second, a negative number is returned.  If the first string is greater, a positive number is returned.  If the strings are equal, the return value is zero.

 int n = string.Compare("Sean", "Steinbeck");    // -1
 n = string.Compare("Sean", "Bozo");             // 1
 n = string.Compare("Sean", "Sean");             // 0
 n = string.Compare("Sean", "sean");             // 1 ("S" > "s")

You can also use the CompareTo instance method of the string type:

 int n = "Sean".CompareTo("Giotto");   // 1 (Sean > Giotto)

You can also ignore case during the comparison:

 n = string.Compare("Sean", "sean", true);         // 0