VB.net 2010 视频教程 VB.net 2010 视频教程 python基础视频教程
SQL Server 2008 视频教程 c#入门经典教程 Visual Basic从门到精通视频教程
当前位置:
首页 > 编程开发 > c#教程 >
  • C#教程之C#教程之C#中的split的基本用法

本站最新发布   C#从入门到精通
试听地址  
https://www.xin3721.com/eschool/CSharpxin3721/

split的使用:

1、使用char()字符分隔:根据单个的char()类型的进行分隔

    代码如下:

string str="e2kdk2fjod2fiksf21";
string [] sArray=str.Split('2');//因为是用char类型分隔字符,使用一定要用单引号
//sArray的值:sArray[0]="e",sArray[1]="kdk",sArray[2]="fjod",sArray[3]="fiksf",sArray[4]="1";

  

2、利用多个字符来分隔字符串

      代码如下:

string str= "TGEDTC_GUIZHISHENG_GUIYHI";
string [] sArray=str.Split(new char[] {'J','G' });//等同于str.Split(new char[2] {'J','G' })
//sArray值:sArray[0]="T",sArray[1]="EDTC_",sArray[2]="UIZHISHEN",sArray[3]="_",sArray[4]="UIYHI"

3、用字符串进行分隔

   使用字符串进行分隔需要借助C#的正则表达式,也就是需要使用Regex类。在Regex的Split方法中:第一个参数为需要分隔的字符串、第二个参数是分隔标识的正则表达式、第三个参数是可选参数。

     代码如下:

string str= "TGEDTC_GUIZHISHENG_GUIYHI";
string [] sArray=Regex.Split(str, "UI");
//sArray值:sArray[0]="TGEDTC_G",sArray[1]="ZHISHENG_G",sArray[2]="YHI"

4、利用字符串或字符串组进行分隔

     代码如下:

string str= "TGEDTC_GUIZHISHENG_GUI UIYHI";
//单个字符串
string [] sArray=str.Split(new string[] { "UI" }, StringSplitOptions.RemoveEmptyEntries);
//sArray值:sArray[0]="TGEDTC_G",sArray[1]="ZHISHENG_G",sArray[2]=" ",sArray[3]="YHI"

String.Split 方法有6个重载函数

程序代码
1) public string[] Split(params char[] separator)
2) public string[] Split(char[] separator, int count)
3) public string[] Split(char[] separator, StringSplitOptions options)
4) public string[] Split(string[] separator, StringSplitOptions options)
5) public string[] Split(char[] separator, int count, StringSplitOptions options)
6) public string[] Split(string[] separator, int count, StringSplitOptions options)

下边我们通过一些实例来说明下怎么使用(以下string words = "1,2.3,,4";):
1. public string[] Split(params char[] separator)
程序代码
string[] split = words.Split(new Char[] { ',' });//返回:{"1","2.3","","4"}
string[] split = words.Split(new Char[] { ',', '.' });//返回:{"1","2","3","","4"} 
2. public string[] Split(char[] separator, int count)
程序代码
string[] split = words.Split(new Char[] { ',', '.' }, 2);//返回:{"1","2.3,,4"}
string[] split = words.Split(new Char[] { ',', '.' }, 6);//返回:{"1","2","3","","4"} 
3. public string[] Split(char[] separator, StringSplitOptions options)
程序代码
string[] split = words.Split(new Char[] { ',', '.' }, StringSplitOptions.RemoveEmptyEntries);//返回:{"1","2","3","4"} 不保留空元素
string[] split = words.Split(new Char[] { ',', '.' }, StringSplitOptions.None);//返回:{"1","2","3","","4"} 保留空元素 
4. public string[] Split(string[] separator, StringSplitOptions options)
程序代码
string[] split = words.Split(new string[] { ",", "." }, StringSplitOptions.RemoveEmptyEntries);//返回:{"1","2","3","4"} 不保留空元素
string[] split = words.Split(new string[] { ",", "." }, StringSplitOptions.None);//返回:{"1","2","3","","4"} 保留空元素 
5. public string[] Split(char[] separator, int count, StringSplitOptions options)
程序代码
string[] split = words.Split(new Char[] { ',', '.' }, 2, StringSplitOptions.RemoveEmptyEntries);//返回:{"1","2.3,,4"} 不保留空元素
string[] split = words.Split(new Char[] { ',', '.' }, 6, StringSplitOptions.None);//返回:{"1","2","3","","4"} 保留空元素 
6. public string[] Split(string[] separator, int count, StringSplitOptions options)
程序代码
string[] split = words.Split(new string[] { ",", "." }, 2, StringSplitOptions.RemoveEmptyEntries);//返回:{"1","2.3,,4"} 不保留空元素
string[] split = words.Split(new string[] { ",", "." }, 6, StringSplitOptions.None);//返回:{"1","2","3","","4"} 保留空元素
需要注意的是没有重载函数public string[] Split(string[] separator),所以我们不能像VB.NET那样使用words.Split(","),而只能使用words.Split(',')

 

相关教程