#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
Advertisements

#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