C# Split函数根据特定分隔符分割字符串的操作
作者:IT技术小趣屋 时间:2023-11-07 07:40:25
在C#程序开发过程中,很多时候可能需要将字符串根据特定的分割字符分割成字符或者List集合,例如根据逗号将字符串分割为数组,或者根据竖线将字符串分割成数组,C#中提供了Split()函数来快速将字符串分割成数组形式,如果需要转换为List集合,可在分割完成后使用数组的ToList()方法即可转换为List集合数据。
例如下列例子,将字符str按照逗号分隔成数组。
string str = "A,B,C,D,E,F,G";
string[] strArr = str.Split(',');//根据逗号分隔字符串str
分隔完成之后的得到的数组strArr,取值为
strArr[0]="A",strArr[1]="B",strArr[2]="C",strArr[3]="D",strArr[4]="E",strArr[5]="F"
在有些业务需求里面,可能要求将字符串按照特定的字符分隔为List集合,可在上述的基础上加个ToList即可实现,实现语句如下:
List<string> strList = str.Split(',').ToList();
补充知识:C#实现字符串按多个字符采用Split方法分割得到数组
String字符串如何按多个字符采用Split方法进行分割呢?本文提供VS2005和VS2003的实现方法,VS2005可以用下面的方法:
string agentInfo = userInfo.Attribute19.ToString();
string[] myAgent = agentInfo.Split(new string[] { "$#$" }, StringSplitOptions.None);
if (myAgent.Length == 3)
{
this.QLookUpMyAgent.Text = myAgent[0].ToString();
this.QCalenderStartDate.Value = myAgent[1].ToString();
this.QCalenderEndDate.Value = myAgent[2].ToString();
}
VS2003下用下面的方法:
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
来源:https://www.50bit.cn/News/Index/5251.html
标签:C#,Split,分割,字符串
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
C#并查集(union-find)算法详解
2023-10-27 12:18:54
![](https://img.aspxhome.com/file/2023/0/122680_0s.jpg)
C#/VB.NET实现HTML转为XML的示例代码
2021-08-13 20:46:48
![](https://img.aspxhome.com/file/2023/5/87045_0s.png)
Java完美实现2048小游戏
2023-06-14 12:59:25
Android IntentFilter的匹配规则示例详解
2022-07-11 19:12:29
![](https://img.aspxhome.com/file/2023/7/129157_0s.png)
Javaweb中Request获取表单数据的四种方法详解
2023-10-07 03:07:02
![](https://img.aspxhome.com/file/2023/6/72606_0s.png)
Android实现左右滑动切换图片
2021-06-20 10:51:30
![](https://img.aspxhome.com/file/2023/8/109178_0s.gif)
SpringBoot集成vue的开发解决方案
2023-11-24 20:58:10
![](https://img.aspxhome.com/file/2023/8/59028_0s.png)
C#使用System.Net邮件发送功能踩过的坑
2022-09-09 18:16:11
![](https://img.aspxhome.com/file/2023/3/113693_0s.png)
JAVA并发图解
2023-03-26 03:11:41
![](https://img.aspxhome.com/file/2023/0/91000_0s.png)
Flutter图片与文件选择器使用实例
2023-07-17 09:11:12
![](https://img.aspxhome.com/file/2023/0/106030_0s.png)
第一次使用Android Studio时你应该知道的一切配置(推荐)
2022-01-08 00:49:38
![](https://img.aspxhome.com/file/2023/0/66250_0s.png)
Android编程基于自定义View实现绚丽的圆形进度条功能示例
2023-07-24 17:13:26
![](https://img.aspxhome.com/file/2023/7/84737_0s.png)
android 通知Notification详解及实例代码
2023-06-26 12:11:42
![](https://img.aspxhome.com/file/2023/6/138756_0s.png)
java求100以内的素数示例分享
2021-06-30 21:34:39
Java详细分析讲解泛型
2023-02-05 07:56:02
![](https://img.aspxhome.com/file/2023/9/71639_0s.png)
Java基于JDBC实现事务,银行转账及货物进出库功能示例
2022-03-24 07:06:34
![](https://img.aspxhome.com/file/2023/4/60764_0s.png)
基于RxJava实现酷炫启动页
2023-09-26 21:50:47
![](https://img.aspxhome.com/file/2023/7/86007_0s.gif)
java中的常用集合类整理
2022-08-22 10:35:49
深入理解java三种工厂模式
2022-03-11 06:09:53
C#基于WinForm实现串口通讯
2023-12-26 02:03:39
![](https://img.aspxhome.com/file/2023/1/113751_0s.jpg)