0

I need to split string variable by multiple characters, in my case by " "; Here is my code:

string s_var = "New   String   variable";
string[] s_mas = s_var.Split("   ");

The Split() method isn't working for me, it says that the argument " " is invalid. Hoping you guys know how to solve this issue.

1 Answer 1

4

You're not specifying the correct arguments.

  • If you want to split by a string, you need to specify an array.
  • You also need to specify whether or not to discard empty strings.

Try this:

var s_mas = s_var.Split(new[] { "   " }, StringSplitOptions.None);
Sign up to request clarification or add additional context in comments.

4 Comments

I've always wondered how this works new[] ? does it know that it is array of strings ?
@RoyiNamir you can infer type of array by looking at its arguments. So in this case we know new[] is same as new string[] because we've used string argument.
@RoyiNamir take a look at Implicitly typed arrays

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.