using System; using System.IO; class Program { static void Main(string[] args) { string inputString = "Hello world"; StringReader stringReader = new StringReader(inputString); // Read the first word string firstWord = stringReader.ReadWord(); Console.WriteLine(firstWord); // Read the second word string secondWord = stringReader.ReadWord(); Console.WriteLine(secondWord); } }
Hello world
using System; using System.IO; class Program { static void Main(string[] args) { string inputString = "One Two Three\nFour Five Six"; StringReader stringReader = new StringReader(inputString); while (true) { string word = stringReader.ReadWord(); if (word == null) { break; } Console.WriteLine(word); } } }
One Two Three Four Five SixThis example reads words from a string using the ReadWord method in a loop. The input string has two lines, and the output shows all the words in the string on separate lines. The package library for the System.IO namespace is included in the .NET Framework Class Library.