在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
字符串的处理往往离不开split方法,下面介绍几种split的用法: 1.对单个字符进行分割(注意这里是字符,不是字符串,故只能用单引号‘’) string s=abcdeabcdeabcde; string[] sArray=s.Split('c') ; foreach(string i in sArray) Console.WriteLine(i.ToString()); 输出下面的结果:ab deab deab de 2.对多个字符进行分割 string s="abcdeabcdeabcde"; string[] sArray1=s.Split(new char[3]{'c','d','e'}) ; foreach(string i in sArray1) Console.WriteLine(i.ToString()); 输出下面的结果:ab ab ab 3.使用正则表达式 using System.Text.RegularExpressions; 输出结果:aaa bbb ccc using System.Text.RegularExpressions; string str1="我**是*****一*****个*****教*****师"; string[] sArray=Regex.Split(str1,@[*]+); foreach (string i in sArray) Response.Write(i.ToString()); 输出结果:我是一个教师 注意: 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) 程序代码
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"} 保留空元素 |
2023-10-27
2022-08-15
2022-08-17
2022-09-23
2022-08-13
请发表评论