1.7. The string Type

Once we have read a line of text, we need to separate it into the individual words. The simplest method of doing that is to use the Split() method of string—for example,

string text_line;
string [] text_words;

while (( text_line = freader.ReadLine() ) != null )
{
      text_words = text_line.Split( null );
      // ...
}

Split() returns an array of string elements separated by a set of characters indicated by the user. If Split() is passed null, as it is in our example, it separates the elements of the original string using white space, such as a blank character or a tab. For example, the string

A beautiful fiery bird, he tells her, magical but untamed.

is split into an array of 10 string elements. Three of them, however—bird,

Get C# Primer: A Practical Approach now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.