logo
Учебник_ПОА

Splitting a String into Substrings

Splitting a string into substrings, such as, splitting a sentence into individual words, is a common programming task. The Split() method takes a char array of delimiters, for example, a space character, and returns an array of substrings. You can access this array with foreach, like this:

char[] delimit = new char[] {' '};

string s10 = "The cat sat on the mat.";

foreach (string substr in s10.Split(delimit))

{

System.Console.WriteLine(substr);

}

This code outputs each word on a separate line, like this:

The

cat

sat

on

the

mat.