Click here to Skip to main content
Click here to Skip to main content

String Helpers

, 15 Jun 2005
Rate this:
Please Sign up or sign in to vote.
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.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

Share

About the Author

Marc Clifton

United States United States
Marc is the creator of two open source projects, MyXaml, a declarative (XML) instantiation engine and the Advanced Unit Testing framework, and Interacx, a commercial n-tier RAD application suite.  Visit his website, www.marcclifton.com, where you will find many of his articles and his blog.
 
Marc lives in Philmont, NY.

Comments and Discussions

 
GeneralMy vote of 5 Pinmembergordon_matt1-Dec-10 20:54 
GeneralRe: My vote of 5 PinprotectorMarc Clifton2-Dec-10 3:03 
Questionerror in leftofn function? Pinmembercookster6129-Aug-08 1:23 
AnswerRe: error in leftofn function? PinprotectorMarc Clifton29-Aug-08 4:28 
GeneralExtension Methods PinmvpPhilip Laureano29-Jan-08 14:15 
GeneralRe: Extension Methods PinprotectorMarc Clifton29-Jan-08 14:56 
GeneralThanks PinmemberJ J S14-Sep-07 5:39 
GeneralBetween PinmemberYousef Mannaa3-Apr-07 5:20 
GeneralVery good idea Pinmembercbolz21-Nov-06 10:59 
GeneralMore ... PinmemberErlend Robaye10-Jan-06 22:17 
GeneralFree methods PinmemberNemanja Trifunovic16-Jun-05 4:31 
GeneralRe: Free methods PinprotectorMarc Clifton16-Jun-05 5:15 
GeneralRe: Free methods PinmemberWiebe Tijsma20-Jun-05 5:04 
GeneralExplaination of the reasons/usage missing. PinsitebuilderUwe Keim16-Jun-05 3:12 
GeneralRe: Explaination of the reasons/usage missing. PinprotectorMarc Clifton16-Jun-05 5:14 
GeneralRe: Explaination of the reasons/usage missing. PinsitebuilderUwe Keim16-Jun-05 5:29 
GeneralRe: Explaination of the reasons/usage missing. PinprotectorMarc Clifton16-Jun-05 5:50 
GeneralRe: Explaination of the reasons/usage missing. PinsitebuilderUwe Keim16-Jun-05 6:04 
GeneralVery Innovative Idea PinmemberGaurang Desai15-Jun-05 20:17 
GeneralNice observations Pinmemberreinux15-Jun-05 16:03 
GeneralRe: Nice observations PinsussAntoine Aubry16-Jun-05 3:59 
GeneralRe: Nice observations Pinmemberreinux16-Jun-05 11:34 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.141223.1 | Last Updated 15 Jun 2005
Article Copyright 2005 by Marc Clifton
Everything else Copyright © CodeProject, 1999-2014
Layout: fixed | fluid