csharp Split string

using System.Text.RegularExpressions;

// split a set of words
string s = "there is a cat";
string[] words = s.Split(' ');
foreach (string word in words)

// split using RegEx
string value = "cat\r\ndog\r\nanimal\r\nperson";
string[] lines = Regex.Split(value, "\r\n");
foreach (string line in lines)

// split on directory separator
const string dir = @"C:\Windows\System32\Drivers\Etc\Hosts";
string[] parts = dir.Split('\\');
foreach (string part in parts)

// split word into individual letters
string myString = "hello";
char[] characters = myString.ToCharArray();
foreach (char character in characters)
	Console.WriteLine(character);// outputs the char
	Console.WriteLine(character.ToString());// outputs the string equivalent
Splitting a string and writing out each element.

Updated: Monday 23rd January 2012, 07:42am

There are 0 comments

Leave a comment of your own

Comments are currently closed.