Jacob van Eijk
Jacob van Eijk

Reputation: 21

Don't split between specific characters

Currently, I'm using this code to split a string:

string[] split = source.Split(new char[] { ' ' });

So "The quick /little brown/ fox" would be: The quick /little brown/ fox

And what I want is this: The quick /little brown/ fox

I have seen some solutions (and I don't really understand them) but they all go into a var and I want it to go into a string[].

Upvotes: 0

Views: 255

Answers (1)

lahsrah
lahsrah

Reputation: 9173

You can use Regex for this:

string s = "The quick /little brown/ fox";
string[] result = Regex.Matches(s, @"((/.+/)|(\b\w+\b))").Cast<Match>().Select(m => m.Value).ToArray();
result.ToList().ForEach(x => Console.WriteLine(x));

Output:

The
quick
/little brown/
fox 

Upvotes: 5

Related Questions