• 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(',')!很多人都很奇怪为什么把双引号改为单引号就可以了?看了上边的重载函数该知道答案了吧^_^

  • 相关阅读:
    MVC4怎么设置@Html.TextBox这样的输入框的css样式
    MVC4怎么设置@Html.TextBoxFor这样的输入框的css样式
    asp.net mvc视图中嵌套分部视图
    asp.net mvc 简单搜索功能
    ASP.NET MVC 中将数据从View传递到控制器中的表单提交法
    bigdecimal更精确的浮点处理方式
    序列化和反序列化
    OutOfMemoryError内存不足
    Java异常分类及处理
    yii加载自带验证码的方法
  • 原文地址:https://www.cnblogs.com/lizhenlin/p/6242530.html
Copyright © 2020-2023  润新知