Fun With Extension Methods - Extending String to Provide a Better Split Method

Do you feel that the standard Split implementation of the String class is a bit awkward to use? Do you keep forgetting you have to declare a char array for the separator? And do you often need to split on multiple characters, like \r\n to split on a line break? In that case, read on. A simple extension method might fix that for you.

Creating a Better Split Method

With the Split method on the String class, splitting a string in multiple lines separated by a \r would look like this:

char [] splitChar = { '\r' };
string source = "Text of First Line\rText of Second Line";
string[] result = source.Split(splitChar);

You define a char array that holds the line break character (\r) and then call Split. Not very intuitive. Also, with this implementation, it's impossible to split on multiple characters, like \r\n - the line break often used on Windows systems.

Normally, you would fix this by calling the Split method of the Regex class in the System.Text.RegularExpressions namespace. However, I often find that people don't know about this class or namespace or find them hard to discover.

With a simple extension method, you can combine the best of both worlds: extend the String class with a Split method that accepts one or more characters in a string. The method itself could easily delegate to the Regex class. To implement this, all you need is some code like this:

namespace MyExtensionMethods
{
  public static class StringExtensions
  {
    /// <summary>
    /// Returns a string array that contains the substrings in this  
    /// string that are delimited by the specified string.		
    /// </summary>
    /// <param name="input">The input string to split.</param>
    /// <param name="separator">The string to split on like \r\n.</param>
    public static string[] Split(this string input, string separator)
    {
      return System.Text.RegularExpressions.Regex.Split(input, separator);
    }
  }
}

With this code, all you need to do is add a using or Imports statement to your code for the MyExtensionMethods namespace, and then you can use Split like this:

string splitChars = "\r\n";
string source = "Text of First Line\r\nText of Second Line";
string[] result = source.Split(splitChars);

This gives you access to the Split method right where you'd expect it: directly on the String class.


Where to Next?

Wonder where to go next? You can post a comment on this article.

Doc ID 440
Full URL https://imar.spaanjaars.com/440/fun-with-extension-methods-extending-string-to-provide-a-better-split-method
Short cut https://imar.spaanjaars.com/440/
Written by Imar Spaanjaars
Date Posted 03/02/2008 15:46
Listened to when writing Eat Yourself by Goldfrapp (Track 5 from the album: Seventh Tree)

Comments

Talk Back! Comment on Imar.Spaanjaars.Com

I am interested in what you have to say about this article. Feel free to post any comments, remarks or questions you may have about this article. The Talk Back feature is not meant for technical questions that are not directly related to this article. So, a post like "Hey, can you tell me how I can upload files to a MySQL database in PHP?" is likely to be removed. Also spam and unrealistic job offers will be deleted immediately.

When you post a comment, you have to provide your name and the comment. Your e-mail address is optional and you only need to provide it if you want me to contact you. It will not be displayed along with your comment. I got sick and tired of the comment spam I was receiving, so I have protected this page with a simple calculation exercise. This means that if you want to leave a comment, you'll need to complete the calculation before you hit the Post Comment button.

If you want to object to a comment made by another visitor, be sure to contact me and I'll look into it ASAP. Don't forget to mention the page link, or the Doc ID of the document.

(Plain text only; no HTML or code that looks like HTML or XML. In other words, don't use < and >. Also no links allowed.