String Split Method Overview

String Split
Split() method Returns a string array that contains the substrings that are delimited by elements of a specified string or Unicode character array are given in Split method parameter.
Split string with Char
class Program
{
static void Main(string[] args)
{
string strOrginal = "Welcome to Asp.Net" ;
string[] strArray = strOrginal.Split(' ');
foreach (string str in strArray)
{
Console.WriteLine(str);
}
Console.ReadLine();
}
}
Out Put
Welcome
to
Asp.Net

Split string with Char[] - Split(char[] separator)
public class Program
{
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);
}
}
}
Out Put
This
is
       a 
       list 
       of 
       words 
       with 
       a 
       bit 
       of 
       punctuation 
       and 
       a 
       tab 
       character

String value
Separator
Returned array
"42, 12, 19"
new Char[] {',', ' '}
{"42", "", "12", "", "19"}
"42..12..19"
new Char[] {'.'}
{"42", "", "12", "", "19"}
"Banana"
new Char[] {'.'}
{"Banana"}
"Darb\nSmarba"
new Char[] {}
{"Darb", "Smarba"}
"Darb\nSmarba"
null
{"Darb", "Smarba"}
 
Split string with Char[] And Restrict Number of Substring - Split(char[] separator,int count)

Parameters
separator
Type: System.Char[]
An array of Unicode characters that delimit the substrings in this instance, an empty array that contains no delimiters, or null.
count
Type: System.Int32
The maximum number of substrings to return.
Return Value
Type: System.String []
An array whose elements contain the substrings in this instance that are delimited by one or more characters in
separator.
Example:
class Program
{
public static void Main()
{
string delimStr = " ,.:" ;
char[] delimiter = delimStr.ToCharArray();
string words = "one two,three:four." ;
string[] split = null;

Console.WriteLine( "The delimiters are -{0}-" , delimStr);
for (int x = 1; x <= 5; x++)
{
split = words.Split(delimiter, x);
Console.WriteLine("\ncount = {0,2} ..............", x);
foreach (string s in split)
{
Console.WriteLine("-{0}-", s);
}
}
}
}
The delimiters are - ,.:- 
       count =  1 .............. 
          -one two,three:four.- 
       count =  2 .............. 
          -one- 
          -two,three:four.- 
       count =  3 .............. 
         -one- 
         -two- 
         -three:four.- 
       count =  4 .............. 
         -one- 
         -two- 
         -three- 
         -four.- 
       count =  5 .............. 
          -one- 
          -two- 
         -three- 
          -four- 
          --         

Split string with Char[] And StringSplitOptions  -Split(char[] separator,StringSplitOptions options)
Parameters
separator
Type: System.Char[]
An array of Unicode characters that delimit the substrings in this instance, an empty array that contains no delimiters, or null.
options
Type: System.StringSplitOptions
RemoveEmptyEntities to omit empty array elements from the array returned; or None to include empty array elements in the array returned.
Return Value
Type: System.String []
An array whose elements contain the sub strings in this instance that are delimited by one or more characters in
separator.
Example:
class Program
{
static void Main(string[] args)
{
string s1 = ",ONE,,TWO,,,THREE,,";
char[] charSeparators = new char[] { ',' };
string[] result;
Console.WriteLine( "The original string is \"{0}\"." , s1);
Console.WriteLine( "The delimiter character is '{0}'.\n" , charSeparators[0]);
//Split a string delimited by characters and return all elements.
Console.WriteLine("Split a string delimited by characters and return all elements:");
result = s1.Split(charSeparators, StringSplitOptions.None);
Show(result);
// Split a string delimited by characters and return all non-empty elements.
Console.WriteLine( "Split a string delimited by characters and return all non-empty elements:" );
result = s1.Split(charSeparators, StringSplitOptions.RemoveEmptyEntries);
Show(result);
Console.ReadLine();
}

// Display the array of separated strings.
public static void Show(string[] entries)
{
Console.WriteLine( "The return value contains these {0} elements:" , entries.Length);
foreach (string entry in entries)
{
Console.Write("<{0}>", entry);
}
Console.Write("\n\n");
}
}
Out Put:
               The original string is ",ONE,,TWO,,,THREE,,".
         The delimiter character is ','.
         Split a string delimited by characters and return all elements:
         The return value contains these 9 elements:
         <><ONE><><TWO><><><THREE><><>
 
         Split a string delimited by characters and return all non-empty elements:
         The return value contains these 3 elements:
         <ONE><TWO><THREE>
 
Split string with Char[] And StringSplitOptions  - Split(char[] separator,int count,StringSplitOptions options)
Parameters
separator
Type: System.Char[]
An array of Unicode characters that delimit the substrings in this instance, an empty array that
contains no delimiters, or null.
count
Type: System.Int32
The maximum number of substrings to return.
options
RemoveEmptyEntries to omit empty array elements from the array returned; or None to include
empty array elements in the array returned.
Return Value
Type: System.String []
An array whose elements contain the substrings in this instance that are delimited by one or
more characters in separator
example:
class Program
{
static void Main(string[] args)
{
// Customer obj = new Customer();


string s1 = ",ONE,,TWO,,,THREE,,";

char[] charSeparators = new char[] { ',' };
string[] result;

Console.WriteLine("Split a string delimited by characters and return 2
elements:");
result = s1.Split(charSeparators, 2, StringSplitOptions.None);
Show(result);

Console.WriteLine( "Split a string delimited by characters and return 2 non
empty elements:" );
result = s1.Split(charSeparators, 2, StringSplitOptions.RemoveEmptyEntries);
Show(result);

Console.ReadLine();
}

// Display the array of separated strings.
public static void Show(string[] entries)
{
Console.WriteLine( "The return value contains these {0} elements:" , entries.Length);
foreach (string entry in entries)
{
Console.Write("<{0}>", entry);
}
Console.Write("\n\n");
}
}
Out Put:
               The original string is ",ONE,,TWO,,,THREE,,".
         The delimiter character is ','.
         Split a string delimited by characters and return 2 elements:
         The return value contains these 2 elements:
         <><ONE,,TWO,,,THREE,,>
 
         Split a string delimited by characters and return 2 non-empty elements:
         The return value contains these 2 elements:
         <ONE><TWO,,,THREE,,>
 
Split string with String[] And StringSplitOptions  - Split(string [] separator, StringSplitOptions options)
Parameters
separator
Type: System.String[]
An array of strings that delimit the substrings in this string, an empty array that contains no
delimiters, or null.
options
Type: System.StringSplitOptions
RemoveEmptyEntries to omit empty array elements from the array returned; or None to
include empty array elements in the array returned.
Return Value
Type: System.String []
An array whose elements contain the substrings in this instance that are delimited by one or more
characters in separator . For more information, see the Remarks section.
 
Split string with Char[] And StringSplitOptions  - Split(ctring[] separator,int count,StringSplitOptions options)
Parameters
separator
Type: System.String[]
An array of strings that delimit the substrings in this string, an empty array that contains no delimiters,
or null
count
Type: System.Int32
The maximum number of substrings to return.

option
Type: System.StringSplitOptions
RemoveEmptyEntries to omit empty array elements from the array returned;or None to include
empty array elements in the array returned.
Return Value
Type: System.String []
An array whose elements contain the substrings in this instance that are delimited by one or more
characters in separator

No comments:

Post a Comment