[英]Split comma-separated values
I am using Visual Studio 2005 and C# 2.0, and I am trying to split a comma-separated string using the string.Split
function 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?我收到一条错误消息,指出无法识别该表达式——我该如何解决这个问题?
.NET 2.0 does not support LINQ - SO thread ; .NET 2.0 不支持 LINQ- SO 线程;
But you can create a 3.5 project in VS2005 - MSDN thread但是可以在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();
}
.NET 2.0 does not use lambda expressions. .NET 2.0 不使用 lambda 表达式。 You need to compile to .NET 3.0 to use them.您需要编译到 .NET 3.0 才能使用它们。
A way to do this without Linq & Lambdas一种无需 Linq 和 Lambdas 的方法
string source = "a,b, b, c";
string[] items = source.Split(new char[] { ',', ' ' }, StringSplitOptions.RemoveEmptyEntries);
Lamba expression aren't included in c# 2.0 C# 2.0 中不包含 Lamba 表达式
maybe you could refert to this post here on SO也许你可以在 SO 上参考这篇文章
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";
}
}
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。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.