views:

103

answers:

3

Basically, I want to be able to use string.Split(char[]) without actually defining a char array as a separate variable. I know in other languages you could do like string.split([' ', '\n']); or something like that. How would I do this in C#?

+1  A: 

It's not pretty, but: string.Split(new char[] { ' ', '\n' });

Chris Schmich
Note that in C# 3 you can make this slightly prettier by eliding the "char". The compiler will work out that new[]{x,y,z} means "new array of the best common type of x, y and z".
Eric Lippert
Awesome, good to know. I'm guessing this is possible because of the type inference work done for LINQ.
Chris Schmich
+1  A: 

you can use this overload:

public String [] Split(params char [] separator)

like this:

yourstring.Split(' ', '\n')
CD
+4  A: 

Here's a really nice way to do it:

string[] s = myString.Split("abcdef".ToCharArray());

The above is equivalent to:

string[] s = myString.Split('a', 'b', 'c', 'd', 'e', 'f');
Dan Tao