3

I trip on C#'s String.Join and String.Split parameter types every time. The problem is that String.Split takes a params char[] array of characters and String.Join takes a string.

Does anyone have a good mnemonic that can help me remember which takes which so I do not have to fix this every single time I do a string manipulation?

[EDIT, since everyone seems to be confused why I don't have IDE support]

I'm using LinqPad, like this, when I use this most of the time:

String.Join("\n", @"LongRawString
WithPlentyOfLines
UsuallyGeneratedBySomeoneElse
OrProducedBySqlServerForExample".Split('\n').Select(x => 
    {
        x = x.Trim();

        //create line of code, like:
        return "int longRawStringIdx = reader.GetOrdinal(\"LongRawString\")";
    }))

I don't get IDE support in LinqPad, and would like to save the few seconds it takes me to go back and fix it every time. It's silly, but then so are half the scripts that we programmers write to automate the 12 second tasks we do every day.

The problem is that I screw up whether or not I'm supposed to be giving Split the string or Join the string as its first parameter (or only in the case of Split).

Chris Pfohl
  • 18,220
  • 9
  • 68
  • 111
  • 1
    What exactly is your problem? The IDE shows the accepted parameters... – Daniel Hilgarth Mar 15 '11 at 11:58
  • You could always define your own extension methods and make it work however you want – Blorgbeard Mar 15 '11 at 11:59
  • String.Join is static, the other is not, isn't it enough that you know that? – Lasse V. Karlsen Mar 15 '11 at 12:00
  • 1
    There's more than one situation where you don't get IDE support. For example I use linq pad to generate code from other data (line by line manipulation with C#'s regex support is way more powerful than Notepad++, for example). But, when you're working in LinqPad you don't have Intellisense unless you pay for it (which I haven't yet). – Chris Pfohl Mar 15 '11 at 12:00
  • You still didn't specify what your problem is. Are you using string instead of char or the other way around or something else completely? – Daniel Hilgarth Mar 15 '11 at 12:02
  • Have you considered paying the modest fee for autocompletion in LINQPad? See http://www.linqpad.net/Purchase.aspx – Jon Skeet Mar 15 '11 at 12:18

1 Answers1

5

You could think of it this way: you're specifying one thing to join on, but several possible delimiters to split on. It makes sense that way round as the input string could already have many delimiters, but it doesn't make sense to join on multiple delimiters, as the Join method would have to work out which delimiter you meant each time.

I'd just use Intellisense though :)

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
  • That's very helpful. Thanks Jon. I came from a C++/Python NLP background where we either rolled our own Split/Join (in C++, since we were dealing with enormous amounts of data), or we used the Python Split/Join, which are so quirky that I remembered them immediately. – Chris Pfohl Mar 15 '11 at 12:14