Monday, June 11, 2018

C# Substring Extensions

Most of the time when I'm using Substring for something, what I'm actually trying to achieve is getting the contents of the string between two other well-known parts. For example, in a query string I might have something like this: http://mysite.com/page?user=mickey&location=Disney&bestie=Donald. If I want to get the location from that string (and yes, I know there are utilities specifically designed to get values out of query strings, but this is the best example I could come up with on short notice so get over it) I'd have to find "location" in the string, then find the next & and then get the contents between them.

Unfortunately, the out-of-the-box versions of Substring only allow us to specify the beginning index or the beginning index and a length. If I don't know the length then I have to do some mucking about with the contents and... you know what, you've probably dealt with this before. I finally decided to write some extension methods for Substring (well, for strings) that take the string I'm searching for and give me back what I want.
   1:  public static class StringExtensions
   2:  {
   3:      public static string Substring(this string input, string searchText, StringComparison comparisonType = StringComparison.InvariantCultureIgnoreCase)
   4:      {
   5:          // find the first occurrence of the search text
   6:          var index = input.IndexOf(searchText, comparisonType);
   7:  
   8:          // either return null or the entire string that comes after the search text
   9:          // append the length of the search string to the index so the search string isn't included in the result
  10:          return index == -1 ? null : input.Substring(index + searchText.Length);
  11:      }
  12:  
  13:      public static string Substring(this string input, string primarySearchText, string secondarySearchText,
  14:          StringComparison comparisonType = StringComparison.InvariantCultureIgnoreCase)
  15:      {
  16:          // find the first occurrence of the primary search text
  17:          var index = input.IndexOf(primarySearchText, comparisonType);
  18:  
  19:          if (index == -1)
  20:          {
  21:              // if the primary search text doesn't exist in the string, just return null
  22:          }
  23:  
  24:          // append the length of the primary search string to the index so the search string isn't included in the result
  25:          index += primarySearchText.Length;
  26:  
  27:          // find the first occurrence of the secondary search text
  28:          var searchUntilIndex = input.IndexOf(secondarySearchText, index, comparisonType);
  29:          // if the secondary search text doesn't exist in the string (after the primary search text occurs),
  30:          // return the entire string that comes after the primary search text
  31:          // if the secondary search text does exist in the string after the primary search text occurs,
  32:          // return whatever is between the primary search text and the secondary search text
  33:          var length = searchUntilIndex == -1 ? input.Length - index : searchUntilIndex - index;
  34:  
  35:          return input.Substring(index, length);
  36:      }
  37:  }

These new methods let me do this:
var location = input.Substring("location=", "&");

and what I'll get back from that call is "Disney" (without the quotes, of course).

I've probably written some form of this over a dozen times. At least now I've saved it for future reference so I can copy/paste it next time I need it.

No comments:

Post a Comment