• C#中Split用法


     

    1、用字符串分隔: 
    using System.Text.RegularExpressions;
    string str="aaajsbbbjsccc";
    string[] sArray=Regex.Split(str,"js",RegexOptions.IgnoreCase);
    foreach (string i in sArray) Response.Write(i.ToString() + "<br>");
    输出结果:
    aaa
    bbb
    ccc
    2、用多个字符来分隔:
    string str="aaajbbbscccjdddseee"; 
    string[] sArray=str.Split(new char[2{'j','s'}); 
    foreach(string i in sArray) Response.Write(i.ToString() + "<br>"); 
    输出结果:
    aaa
    bbb
    ccc
    ddd
    eee
    3、用单个字符来分隔: string str="aaajbbbjccc";
    string[] sArray=str.Split('j');
    foreach(string i in sArray) Response.Write(i.ToString() + "<br>");
    输出结果:
    aaa
    bbb
    ccc
     
    string[] arr = str.Split("o");

    这是一个具有语法错误的语句,Split 的 separator 参数应该是 char[] 或 string[],不应是字符串。正确的示例:

    string str = "technology"; char[] separator = { 'o' }; string[] arr = str.Split(separator);
     
     
    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)ITPUB个人空间,n:H!C0M/S3Uu0002P
    程序代码
    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"} 不保留空元素 u0002w1I+Ch%^u0017}0string[] 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"} 不保留空元素ITPUB个人空间1K;eu0007fu0008f }u0011C n 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(',')
     
     
    转载来源:http://www.cnblogs.com/yugen/archive/2010/08/18/1802781.html
     
  • 相关阅读:
    【C#】:浅谈反射机制 【转】
    ArcGIS Server 10中的切图/缓存机制深入【转】
    ArcGIS Server的切图原理深入【转】
    【OpenGL】用OpenGL shader实现将YUV(YUV420,YV12)转RGB-(直接调用GPU实现,纯硬件方式,效率高)
    MFC 带Ribbonbar的窗口 实现全屏和取消全屏
    C#.net开发 List与DataTable相互转换 【转】
    Net编程 详解DataTable用法【转】
    三维空间两直线/线段最短距离、线段计算算法 【转】
    OSG立体模式下动态修改相机远近裁剪面的实现
    用curl去探测接口是否正常返回结果,若没有正常返回则触发报警
  • 原文地址:https://www.cnblogs.com/hao-1234-1234/p/6101634.html
Copyright © 2020-2023  润新知