Click here to Skip to main content
11,581,515 members (63,248 online)
Click here to Skip to main content

String Helpers

, 15 Jun 2005 76.9K 428 61
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.

You may also be interested in...

Comments and Discussions

 
GeneralMy vote of 5 Pin
gordon_matt1-Dec-10 19:54
membergordon_matt1-Dec-10 19:54 
GeneralRe: My vote of 5 Pin
Marc Clifton2-Dec-10 2:03
protectorMarc Clifton2-Dec-10 2:03 
Questionerror in leftofn function? Pin
cookster6129-Aug-08 0:23
membercookster6129-Aug-08 0:23 
AnswerRe: error in leftofn function? Pin
Marc Clifton29-Aug-08 3:28
protectorMarc Clifton29-Aug-08 3:28 
GeneralExtension Methods Pin
Philip Laureano29-Jan-08 13:15
mvpPhilip Laureano29-Jan-08 13:15 
GeneralRe: Extension Methods Pin
Marc Clifton29-Jan-08 13:56
protectorMarc Clifton29-Jan-08 13:56 
GeneralThanks Pin
J J S14-Sep-07 4:39
memberJ J S14-Sep-07 4:39 
GeneralBetween Pin
Yousef Mannaa3-Apr-07 4:20
memberYousef Mannaa3-Apr-07 4:20 
GeneralVery good idea Pin
cbolz21-Nov-06 9:59
membercbolz21-Nov-06 9:59 
GeneralMore ... Pin
Erlend Robaye10-Jan-06 21:17
memberErlend Robaye10-Jan-06 21:17 
GeneralFree methods Pin
Nemanja Trifunovic16-Jun-05 3:31
memberNemanja Trifunovic16-Jun-05 3:31 
GeneralRe: Free methods Pin
Marc Clifton16-Jun-05 4:15
protectorMarc Clifton16-Jun-05 4:15 
GeneralRe: Free methods Pin
Wiebe Tijsma20-Jun-05 4:04
memberWiebe Tijsma20-Jun-05 4:04 
GeneralExplaination of the reasons/usage missing. Pin
Uwe Keim16-Jun-05 2:12
sitebuilderUwe Keim16-Jun-05 2:12 
GeneralRe: Explaination of the reasons/usage missing. Pin
Marc Clifton16-Jun-05 4:14
protectorMarc Clifton16-Jun-05 4:14 
GeneralRe: Explaination of the reasons/usage missing. Pin
Uwe Keim16-Jun-05 4:29
sitebuilderUwe Keim16-Jun-05 4:29 
GeneralRe: Explaination of the reasons/usage missing. Pin
Marc Clifton16-Jun-05 4:50
protectorMarc Clifton16-Jun-05 4:50 
GeneralRe: Explaination of the reasons/usage missing. Pin
Uwe Keim16-Jun-05 5:04
sitebuilderUwe Keim16-Jun-05 5:04 
GeneralVery Innovative Idea Pin
Gaurang Desai15-Jun-05 19:17
memberGaurang Desai15-Jun-05 19:17 
GeneralNice observations Pin
reinux15-Jun-05 15:03
memberreinux15-Jun-05 15:03 
GeneralRe: Nice observations Pin
Antoine Aubry16-Jun-05 2:59
sussAntoine Aubry16-Jun-05 2:59 
GeneralRe: Nice observations Pin
reinux16-Jun-05 10:34
memberreinux16-Jun-05 10: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
Web04 | 2.8.150603.1 | Last Updated 15 Jun 2005
Article Copyright 2005 by Marc Clifton
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid