65.9K
CodeProject is changing. Read more.
Home

String Helpers

starIconstarIconstarIconstarIcon
emptyStarIcon
starIcon

4.40/5 (23 votes)

Jun 15, 2005

5 min read

viewsIcon

110403

downloadIcon

703

A small set of helper methods for string manipulation.

Introduction

For almost all my applications, I end up having to do some sort of string manipulation. The String class doesn't support easy string manipulation. The only methods it provides that are of any use for parsing a string are IndexOf, Substring, and Split. Substring is very useful, but it takes integers, so you have to apply IndexOf first to obtain the index of a character inside the string. The result is messy code and a lot of error checking. I've designed the class presented in this article to improve the readability of your code and to define specific behaviors when IndexOf returns -1.

The methods

All of the methods in the StringHelpers class are static methods. The methods are:

  • LeftOf (two overloads)
  • RightOf (two overloads)
  • LeftOfRightmostOf
  • RightOfRightmostOf
  • Between
  • Count
  • Rightmost

LeftOf

Returns the string to the left of the first occurrence of [c] in the [source] string.

Parameters

  • source string,
  • character to find in the source string.

Behavior

Returns a string of all characters to the left of the search character. If the character cannot be found in the source string, the entire string is returned. The rationale here is that the entire string is scanned for the search character, and, not being found, the method returns all the characters scanned.

Implementation

public static string LeftOf(string src, char c)
{
  string ret=src;
  int idx=src.IndexOf(c);
  if (idx != -1)
  {
    ret=src.Substring(0, idx);
  }
  return ret;
}

LeftOf [n]

Returns the string to the left of the [nth] occurrence of [c] in the [source] string.

Parameters

  • source string,
  • character [c] to find in the source string,
  • the nth instance of c to find.

Behavior

Returns a string of all characters to the left of the nth occurrence of the search character. If the character cannot be found in the source string, the entire string is returned. The rationale here is that the entire string is scanned for the search character, and, not being found, the method returns all the characters scanned.

Implementation

public static string LeftOf(string src, char c, int n)
{
  string ret=src;
  int idx=-1;
  while (n > 0)
  {
    idx=src.IndexOf(c, idx+1);
    if (idx==-1)
    {
      break;
    }
  }
  if (idx != -1)
  {
    ret=src.Substring(0, idx);
  }
  return ret;
}

RightOf

Returns the string to the right of the first occurrence of [c] in the [source] string.

Parameters

  • source string,
  • character to find in the source string.

Behavior

Returns a string of all characters to the right of the search character. If the character cannot be found in the source string, an empty string is returned. The rationale here is that the entire string is scanned for the search character, but until the search character is found, these characters are ignored. Therefore, if the search character is not found, the method returns an empty string.

Implementation

public static string RightOf(string src, char c)
{
  string ret=String.Empty;
  int idx=src.IndexOf(c);
  if (idx != -1)
  {
    ret=src.Substring(idx+1);
  }
  return ret;
}

RightOf [n]

Returns the string to the right of the [nth] occurrence of [c] in the [source] string.

Parameters

  • source string,
  • character to find in the source string,
  • the nth instance of [c] to find.

Behavior

Returns a string of all characters to the right of the nth occurrence of the search character. If the character cannot be found in the source string, an empty string is returned. The rationale here is that the entire string is scanned for the search character, but until the search character is found, these characters are ignored. Therefore, if the search character is not found, the method returns an empty string.

Implementation

public static string RightOf(string src, char c, int n)
{
  string ret=String.Empty;
  int idx=-1;
  while (n > 0)
  {
    idx=src.IndexOf(c, idx+1);
    if (idx==-1)
    {
      break;
    }
    --n;
  }

  if (idx != -1)
  {
    ret=src.Substring(idx+1);
  }

  return ret; 
}

LeftOfRightmostOf

Returns the string to the left of the rightmost occurrence of the search character.

Parameters

  • source string,
  • character to find in the source string.

Behavior

Returns a string of all characters to the left of the rightmost occurrence of the search character. If the character cannot be found in the source string, the entire string is returned. The rationale here is that the entire string is scanned for the search character, and, not being found, the method returns all the characters scanned.

Implementation

public static string LeftOfRightmostOf(string src, char c)
{
  string ret=src;
  int idx=src.LastIndexOf(c);
  if (idx != -1)
  {
    ret=src.Substring(0, idx);
  }
  return ret;
}

RightOfRightmostOf

Returns the string to the right of the rightmost occurrence of the search character.

Parameters

  • source string,
  • character to find in the source string.

Behavior

Returns a string of all characters to the right of the rightmost occurrence of the search character. If the character cannot be found in the source string, an empty string is returned. The rationale here is that the entire string is scanned for the search character, but until the search character is found, these characters are ignored. Therefore, if the search character is not found, the method returns an empty string.

Implementation

public static string RightOfRightmostOf(string src, char c)
{
  string ret=String.Empty;
  int idx=src.LastIndexOf(c);
  if (idx != -1)
  {
    ret=src.Substring(idx+1);
  }
  return ret;
}

Between

Returns the string between and exclusive of two search characters.

Parameters

  • The source string.
  • The character to find that demarks the start of the substring, exclusive of the search character.
  • The character to find that demarks the end of the substring, exclusive of the search character.

Behavior

Returns the string that is between the two specified search characters. The returned string excludes the search characters. If the starting character is not found, an empty string is returned. If the ending character is not found after the starting character, an empty string is returned.

Implementation

public static string Between(string src, char start, char end)
{
  string ret=String.Empty;
  int idxStart=src.IndexOf(start);
  if (idxStart != -1)
  {
    ++idxStart;
    int idxEnd=src.IndexOf(end, idxStart);
    if (idxEnd != -1)
    {
      ret=src.Substring(idxStart, idxEnd-idxStart);
    }
  }
  return ret;
}

Count

Returns the number of occurrences of the specified character in the source string.

Parameters

  • The source string.
  • The character of which to count occurrences.

Behavior

Returns 0 if no occurrences of the search character are found in the source string.

Implementation

public static int Count(string src, char find)
{
  int ret=0;
  foreach(char s in src)
  {
    if (s==find)
    {
      ++ret;
    }
  }
  return ret;
}

Rightmost

Returns the rightmost character in the source string.

Parameters

  • The source string.

Behavior

Returns '\0' if the string is empty.

Implementation

public static char Rightmost(string src)
{
  char c='\0';
  if (src.Length>0)
  {
    c=src[src.Length-1];
  }
  return c;
}

Unit tests

There are 17 unit tests provided, using the Advanced Unit Test (AUT) test runner, downloadable here.

Conclusion

You can see that each of these methods is very simple and very short. For more complicated string parsing, I would suggest the Regex class. But for most of my needs, this class works quite well, improving code readability.