In C#, you can split a string into an array of substrings using Split(). It takes the separator character (or string) to split on:
s.Split(separator) // splits using a separator
Here are some examples:
string sentence = "hello world foo";
string[] words = sentence.Split(' ');
// words == ["hello", "world", "foo"]
string csv = "a,b,c";
string[] parts = csv.Split(',');
// parts == ["a", "b", "c"]
You can loop through the resulting array:
foreach (string w in words) {
Console.WriteLine(w);
}
Assignment
Follow the Coding Tutorial and split sentence into a string array!
Hint
Use sentence.Split(' ') and assign the result to words.
Free plan: 30 tutor messages. PRO is unlimited.
Saved
Ctrl + Enter to check
