String class provides the Split method that is used to split a string delimited with some specified characters. It identifies the substrings that are delimited by one or more characters specified in an array, and then return these substrings in a string array. Delimiter characters are not included in the substrings.
The Split function has forms like this:
- string[] Split(params char[])
- string[] Split(params char[] separator)
The Split function returns one of these:
- An array consisting of a single element containing the entire string if the string contains none of the characters in the separator list.
- An array of substrings if the string is delimited by one or more of the characters specified in the separator list passed in the Split method.
- An array of substrings in a string delimited by white space characters if those characters occur and the separator array passed is a null reference or contains no delimiter characters.
- String.Empty (a static read-only field that represents the empty string) where two separators are adjacent or a separator is found at the beginning or end of the string.
For example, if we want to split string 38,\n29, 57 with a character array separator list containing comma and space characters, we will get "38", "", "29", "", "57" as string array elements returned. Or, if we want to split string "38..29..57" with the character array delimiter list containing a period '.' , we will get "38", "", "29", "", "57" as string array elements.
Here is a complete example of using various separators to split different strings.
Here is a complete example of using various separators to split different strings.
class Program
{
static void Main(string[] args)
{
// Some strings with separated by space, comma, and a substring
string intSpacedString = "1 3 5 7 9";
string charCommaString = "a, e, i, o, u";
string nameCommaString = "Mahesh,Raj,,,Dinesh,Mike,Praveen,Joe,Lana";
string subStringString = "12KKK34KKK56KKK78KKK";
char[] spaceSeparator = new char[] { ' ' };
char[] commaSeparator = new char[] { ',' };
string[] stringSeparators = new string[] { "KKK" };
string[] result;
Console.WriteLine("=======================================");
Console.WriteLine("Space separated strings :\n");
result = intSpacedString.Split(spaceSeparator, StringSplitOptions.None);
foreach (string str in result)
{
Console.WriteLine(str);
}
Console.WriteLine("=======================================");
Console.WriteLine("Comma separated strings :\n");
result = charCommaString.Split(commaSeparator, StringSplitOptions.None);
foreach (string str in result)
{
Console.WriteLine(str);
}
result = nameCommaString.Split(commaSeparator, StringSplitOptions.None);
foreach (string str in result)
{
Console.WriteLine(str);
}
Console.WriteLine("=======================================");
Console.WriteLine("Substring separated strings :\n");
result = subStringString.Split(stringSeparators, StringSplitOptions.None);
foreach (string str in result)
{
Console.WriteLine(str);
}
Console.WriteLine("=======================================");
Console.ReadKey();
}
}
---------------------
The following example demonstrates how to extract individual words from a block of text by treating white space and punctuation marks as delimiters. The character array passed to the separator parameter of the String.Split(Char[]) method consists of a space character and a tab character, together with some common punctuation symbols.
---------------------
The following example demonstrates how to extract individual words from a block of text by treating white space and punctuation marks as delimiters. The character array passed to the separator parameter of the String.Split(Char[]) method consists of a space character and a tab character, together with some common punctuation symbols.
using System;
public class SplitTest {
public static void Main() {
string words = "This is a list of words, with: a bit of punctuation" +
"\tand a tab character.";
string [] split = words.Split(new Char [] {' ', ',', '.', ':', '\t' });
foreach (string s in split) {
if (s.Trim() != "")
Console.WriteLine(s);
}
}
}
// The example displays the following output to the console:
// This
// is
// a
// list
// of
// words
// with
// a
// bit
// of
// punctuation
// and
// a
// tab
// character
No comments:
Post a Comment