Split usage in C # ~

Source: Internet
Author: User

1. Delimited by string:

using System.Text.RegularExpressions; string str="aaajsbbbjsccc"; string [] Sarray=regex.split (str,"js", regexoptions.ignorecase); foreach (stringin"<br>");

Output Result:
Aaa
Bbb
Ccc

2, separated by multiple characters:

 string  str= " aaajbbbscccjdddseee   "  string  [] sarray=str. Split (new  char  [ 2 ] { " j   ", "   s   " });  foreach  (string  i in  sarray) Response.Write (i.tostring () + "  <BR>  " ); 

Output Result:
Aaa
Bbb
Ccc
Ddd
Eee

3. Separate the characters with a single character:

string str="aaajbbbjccc"; string [] sarray=str. Split ('J'); foreach (stringin"<br>");

Output Result:
Aaa
Bbb
Ccc

////////////////////////////////////////////////string[] arr = str. Split ("O");

This is a syntax error statement, Split's separator parameter should be char[] or string[] and should not be a string. The correct example:

String str = "Technology";
Char[] Separator = {' O '};
string[] arr = str. Split (separator); The////////////////////////////////////////////////////String.Split method has 6 overloaded functions: Program code
1) Public string[] Split (params Char[] separator)2) Public string[] Split (Char[] Separator,intcount)3) Public string[] Split (Char[] separator, stringsplitoptions options)4) Public string[] Split (string[] separator, stringsplitoptions options)5) Public string[] Split (Char[] Separator,intcount, stringsplitoptions options)6) Public string[] Split (string[] Separator,intCount, stringsplitoptions options)
Below we have some examples to illustrate how to use (the following string words = "1,2.3,,4";):
1. Public string[] Split (params char[] separator)Program code
string [] split = words. Split (new',' }); // return: {"1", "2.3", "" "," 4 "} string [] split = words. Split (new','. ' }); //
2. Public string[] Split (char[] separator, int count)Program code
 string  [] split = words. Split (new  char[] { "   ", "  .   "}, 2 ); //  return: {"1", "2.3,,4"}  string  [] split = words. Split (new  char[] { "   ", "  .   "}, 6 ); //  return: {"1", "2", "3", "", "4"}  
3. Public string[] Split (char[] separator, stringsplitoptions options)Program code
 string  [] split = words. Split (new  char[] { "   ", "  .   "}, Stringsplitoptions.removeemptyentries); //  return: {"1", "2", "3", "4"} do not preserve empty elements  string  [] split = words. Split (new  char[] { "   ", "  .   "}, Stringsplitoptions.none); //  return: {"1", "2", "3", "", "4"} Leave empty element  
4. Public string[] Split (string[] separator, stringsplitoptions options)Program code
string[] split = words. Split (New string[] {",","."}, Stringsplitoptions.removeemptyentries);//return: {"1", "2", "3", "4"} do not preserve empty elementsstring[] split = words. Split (New string[] {",","."}, Stringsplitoptions.none);//returned: {"1", "2", "3", "", "4"} Reserved Empty element
5. Public string[] Split (char[] separator, int count, stringsplitoptions options)Program code
string[] split = words. Split (NewChar[] {',','.'},2, stringsplitoptions.removeemptyentries);//returned: {"1", "2.3,,4"} does not preserve empty elementsstring[] split = words. Split (NewChar[] {',','.'},6, Stringsplitoptions.none);//returned: {"1", "2", "3", "", "4"} Reserved Empty element
6. Public string[] Split (string[] separator, int count, stringsplitoptions options)Program code
string[] split = words. Split (New string[] {",","."},2, stringsplitoptions.removeemptyentries);//returned: {"1", "2.3,,4"} does not preserve empty elementsstring[] split = words. Split (New string[] {",","."},6, Stringsplitoptions.none);//returned: {"1", "2", "3", "", "4"} Reserved Empty element

Note that there is no overloaded function public string[] Split (string[] separator), so we cannot use Words.split (",") like vb.net, and only use words. Split (', ')

Turn: Not far from the column in C # split usage ~

Split usage in C # ~

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.