Start learning free

LESSON 63 OF 674 · STRINGS I

String Split in C#

Lesson

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.

STEP 1 OF 1
    Ctrl + Enter to check