C# 根据 string \r\n转list
时间: 2024-01-25 18:02:34 浏览: 23
以下是C#根据字符串转换为列表的示例代码:
```csharp
string subject = "语文\r\n数学\r\n英语\r\n";
List<string> list = subject.Split(new string[] { "\r\n" }, StringSplitOptions.RemoveEmptyEntries).ToList();
foreach (string item in list)
{
Console.WriteLine(item);}
```
输出结果为:
```
语文
数学
英语
```
相关问题
C# String 分隔符 转 list
以下是两种C#将字符串分隔符转为列表的方法:
1. 使用Split方法和foreach循环:
```csharp
string str = "a a a a a\n b b b b b\n";
string[] liststr = str.Split('\n');
List<string> mylist = new List<string>();
foreach (var item in liststr)
{
mylist.Add(item);
}
Console.Write(mylist.Count); // 输出:2
Console.Write(mylist[1]); // 输出: b b b b b
```
2. 使用ToCharArray方法和Split方法:
```csharp
string str1 = "a a a a a\n b b b b b\n";
char[] p = str1.ToCharArray();
Console.WriteLine("data char length:" + p.Length);
string[] liststr1 = str1.Split(p[0]);
List<string> mydata = new List<string>();
foreach (var item in liststr1)
{
mydata.Add(item);
}
```
C#中string字符串转list集合
C是一种编程语言,它是一种高级语言,也是一种通用语言。它由Dennis Ritchie在20世纪70年代初开发,被广泛应用于操作系统、编译器、数据库、网络和游戏等领域。C语言具有高效、灵活、可移植等特点,是学习计算机编程的重要基础。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)