2

So I have a string input like this:

string pianist = "Johann Sebastian Bach";

How to split those by space so I can access such as:

pianist[0] == "Johann"
pianist[1] == "Sebastian"
pianist[2] == "Bach"

I tried

string test = pianist.Split(' ');

but its not working.

sujith karivelil
  • 28,671
  • 6
  • 55
  • 88
iDeal
  • 201
  • 2
  • 4
  • 7
  • How did you tried? Please show us. – SᴇM Nov 03 '17 at 07:13
  • `string[] test = pianist.Split(" ");` => is this what you want? `Split` method turns a string into string array. – Tetsuya Yamamoto Nov 03 '17 at 07:14
  • maybe you can take a look at his answare https://stackoverflow.com/a/8928665/7773041 –  Nov 03 '17 at 07:17
  • 1
    But if it is not working, you get some kind of compile time error, right? Even though the question is already answered below, it is worth mentioning that adding the error message to the question can make it much easier to answer it. – Raimund Krämer Nov 03 '17 at 07:40

9 Answers9

5

you can try this one;

string[] pianist = "Johann Sebastian Bach".Split(' ');

you will get as expected.

arslanaybars
  • 1,813
  • 2
  • 24
  • 29
4

string test = pianist.Split(' '); // Doesn't work Why?

Because the .Split() method of String class Splits a string into substrings that are based on the characters in an array. you cannot assign them to a string, you should use an array instead.

What to do to make your code work?

Change the type of to string[] to hold the result of split:

 string[] splitResult = pianist.Split(' ');
sujith karivelil
  • 28,671
  • 6
  • 55
  • 88
2

You don't need to pass any params of your split method. check interested MSDN post:

If the separator argument is null or contains no characters, the method treats white-space characters as the delimiters. White-space characters are defined by the Unicode standard they return true if they are passed to the Char.IsWhiteSpace method. String.Split Method

string pianist = "Johann Sebastian Bach";
var pianistArray = pianist.Split();

Result:

pianistArray[0] == "Johann"
pianistArray[1] == "Sebastian"
pianistArray[2] == "Bach
arslanaybars
  • 1,813
  • 2
  • 24
  • 29
2

The problem is that your expectation is wrong.

so I can access such as pianist[0] == "Johann"

since pianist is your original string, trying to access it with an index will result in chars at this position in the string. (since a string is represented by a char[]).

If you look at the documentation of the method Split() you will see that it returns a string[] and not a string as you tried. You need first to catch this return value in an extra variable, then you can access it the way you planed:

string pianist = "Johann Sebastian Bach";
string [] returnedArray = pianist.Split(' ');

string johann = returnedArray[0];
string sabastian = returnedArray[1];
string bach = returnedArray[2];
Mong Zhu
  • 23,309
  • 10
  • 44
  • 76
1

You are trying to save a string-array under a string variable. Try this:

string test = "Johann Sebastian Bach";
string[] separated = test.Split(' ');
foreach(string sub in separated)
{
    Console.WriteLine(sub);
}

produces

Johann Sebastian Bach

pfuhlert
  • 533
  • 4
  • 16
0

Split function must specify the value to split. your situation is a space " ", not "". here is the answer

   string pianist = "Johann Sebastian Bach";
    string[] asd = pianist.Split(" ");
i3lai3la
  • 980
  • 6
  • 10
0

It should see below looks like you have not given the space correctly

 var res = pianist.Split(new[] {' '});
ChiragMM
  • 347
  • 4
  • 12
  • "looks like you have not given the space correctly" the way OP is using `Split` is entirely correct. The way of catching the return value and accessing the strings is wrong. – Mong Zhu Nov 03 '17 at 07:20
0
string test = pianist.Split(' ');

This won't work as the result from Split is an array. Do this instead:

string[] test = pianist.Split(' ');

No you can access the words like you already tried to:

test[0]
test[1]
...
Basti
  • 517
  • 4
  • 19
0

Try this:

string pianist = "Johann Sebastian Bach";
var result = pianist.Split(' ');

Or:

string pianist = "Johann Sebastian Bach";
string[] result = pianist.Split(' ');
Willy David Jr
  • 8,604
  • 6
  • 46
  • 57