3

Possible Duplicate:
Why isn’t there a string.Split(string) overload?

There are several overloads but not one that allows sending a string. Which is the very often used by developers, at least by my team.

string[] x = "abc|||dev".Split("|||");

Why?..


Please, I am not asking How to do split passing string.

Community
  • 1
  • 1
RollRoll
  • 8,133
  • 20
  • 76
  • 135

2 Answers2

3
x.Split(new String[] { "|||" }, StringSplitOptions.None);

Regex.Split(x, @"([a-zA-Z]+)\|\|\|([a-zA-Z]+)");

public static class StringExtensions()
{
    public static String[] Split(this String s, String delimiter)
    {        
        return s.Split(new String[] { delimiter }, StringSplitOptions.None);
    }
}
Matthew
  • 24,703
  • 9
  • 76
  • 110
Tommaso Belluzzo
  • 23,232
  • 8
  • 74
  • 98
  • Documentation: http://msdn.microsoft.com/en-us/library/tabh47cf.aspx – Matthew Jan 17 '13 at 21:44
  • 1
    As I said, I'm aware of the overloads. My question is why not accepting the plain string. If there is any idea behind that... – RollRoll Jan 17 '13 at 21:47
  • 1
    This has always annoyed me too. I suppose you could add it as an extension method. – Mike Christensen Jan 17 '13 at 21:49
  • Please read my question... – RollRoll Jan 17 '13 at 21:49
  • It might have been an oversight, as for why Microsoft does not provide one, Eric Lippert wrote a blog post about "Why did you not implement function X", and the answer was that there is a lot of organizational overhead to make library changes for something that can simply be solved by writing it yourself. (Things like documentation, translations, unit-tests, deployment, QA, etc.) – Matthew Jan 17 '13 at 21:50
  • Sorry, I can't explain you why. Probably for convenience as array parameters will be internally iterated to produce splits. – Tommaso Belluzzo Jan 17 '13 at 21:51
  • Matthew, so probably someone missed implementing that in the first place? sounds possible but weird – RollRoll Jan 17 '13 at 21:52
  • Happens all the time, might be why extension methods were born. – Matthew Jan 17 '13 at 21:57
2

I can't say why it isn't included but if you do a lot of string-splitting then an extension-method would be in order to help you with your favorite overload.

class Program
{
    static void Main(string[] args)
    {
        string[] x = "abc|||dev".Split("|||");
    }
}

public static class StringExtensions
{
    public static string[] Split(this string str, string separator)
    {
        return str.Split(new[] { separator }, StringSplitOptions.None);
    }
}
Karl-Johan Sjögren
  • 16,544
  • 7
  • 59
  • 68