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#?
views:
103answers:
3
+1
A:
It's not pretty, but: string.Split(new char[] { ' ', '\n' });
Chris Schmich
2010-05-09 05:06:20
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
2010-05-09 15:09:39
Awesome, good to know. I'm guessing this is possible because of the type inference work done for LINQ.
Chris Schmich
2010-05-09 19:45:56
+1
A:
you can use this overload:
public String [] Split(params char [] separator)
like this:
yourstring.Split(' ', '\n')
CD
2010-05-09 05:08:39
+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
2010-05-09 05:10:01