The.Anti.9
The.Anti.9

Reputation: 44648

C# Implicit array declaration

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#?

Upvotes: 4

Views: 2292

Answers (3)

CD..
CD..

Reputation: 74116

you can use this overload:

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

like this:

yourstring.Split(' ', '\n')

Upvotes: 1

Dan Tao
Dan Tao

Reputation: 128317

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');

Upvotes: 8

Chris Schmich
Chris Schmich

Reputation: 29476

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

Upvotes: 1

Related Questions