C# 拆分逗号分隔值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17571841/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Split comma-separated values
提问by user1292656
I am using Visual Studio 2005 and C# 2.0, and I am trying to split a comma-separated string using the string.Splitfunction and a lambda expression as follows:
我正在使用 Visual Studio 2005 和 C# 2.0,我正在尝试使用string.Split函数和 lambda 表达式拆分逗号分隔的字符串,如下所示:
string s = "a,b, b, c";
string[] values = s.Split(',').Select(sValue => sValue.Trim()).ToArray();
I get an error saying that the expression is not recognized -- how can I resolve this?
我收到一条错误消息,指出无法识别该表达式——我该如何解决这个问题?
采纳答案by Jon G
.NET 2.0 does not support LINQ - SO thread;
But you can create a 3.5 project in VS2005 - MSDN thread
.NET 2.0 不支持 LINQ- SO 线程;
但是可以在VS2005中创建3.5项目——MSDN线程
Without lambda support, you'll need to do something like this:
如果没有 lambda 支持,您需要执行以下操作:
string s = "a,b, b, c";
string[] values = s.Split(',');
for(int i = 0; i < values.Length; i++)
{
values[i] = values[i].Trim();
}
回答by basher
.NET 2.0 does not use lambda expressions. You need to compile to .NET 3.0 to use them.
.NET 2.0 不使用 lambda 表达式。您需要编译到 .NET 3.0 才能使用它们。
回答by Rémi
Lamba expression aren't included in c# 2.0
C# 2.0 中不包含 Lamba 表达式
maybe you could refert to this post here on SO
回答by Cybermaxs
A way to do this without Linq & Lambdas
一种无需 Linq 和 Lambdas 的方法
string source = "a,b, b, c";
string[] items = source.Split(new char[] { ',', ' ' }, StringSplitOptions.RemoveEmptyEntries);
回答by Ryan Weir
You could use LINQBridge(MIT Licensed) to add support for lambda expressions to C# 2.0:
您可以使用LINQBridge(MIT 许可)向 C# 2.0 添加对 lambda 表达式的支持:
With Studio's multi-targeting and LINQBridge, you'll be able to write local (LINQ to Objects) queries using the full power of the C# 3.0 compiler—and yet your programs will require only Framework 2.0.
借助 Studio 的多目标和 LINQBridge,您将能够使用 C# 3.0 编译器的全部功能编写本地(LINQ to Objects)查询,而您的程序只需要 Framework 2.0。
回答by Mahesh Kumar Rajput
Split a Textbox value separated by comma and count the total number of values in text and splitted values are shown in ritchTextBox.
拆分以逗号分隔的 Textbox 值并计算文本中值的总数,拆分后的值显示在 ritchTextBox 中。
private void button1_Click(object sender, EventArgs e)
{
label1.Text = "";
richTextBox1.Text = "";
string strText = textBox1.Text.Trim();
string[] strArr = strText.Split(',');
int count = 0;
for (int i = 0; i < strArr.Length; i++)
{
count++;
}
label1.Text = Convert.ToString(count);
for (int i = 0; i < strArr.Length; i++)
{
richTextBox1.Text += strArr[i].Trim() + "\n";
}
}

