• C#的String.Split方法


    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(',')!

    在一般情况下,某事物个体发生具有其特有属性的负面现象,且无法以科学的角度得到合理有效的解释。我们通常称此类现象为“人品问题”(RPWT)。
                                                   ——摘自《辞海》第314页

  • 相关阅读:
    让Windows Server 2008 + IIS 7+ ASP.NET 支持10万个同时请求
    C#多线程锁定全局变量
    ASP.NET 防止F5刷新页面按钮重复提交
    子线程调用窗体控件
    asp.net 转成XML后在SQL操作XML的值
    Sql中字符串的循环截取(用循环实现输入键串能输出值串)
    大数据量分页存储过程
    函数相关概念,及练习
    js循环(while循环,do while循环,for循环)相关知识点及练习
    parseInt()和Number()的应用区别
  • 原文地址:https://www.cnblogs.com/dflower/p/1599115.html
Copyright © 2020-2023  润新知