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)
{
	Console.WriteLine(word);
}

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

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

// 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, 09:43am

There are 0 comments

Leave a comment of your own

Comments are currently closed.