Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Split comma-separated values

Tags:

c#

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:

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?

like image 532
user1292656 Avatar asked Jul 10 '13 13:07

user1292656


People also ask

How do you split a comma separated string?

To split a string with comma, use the split() method in Java. str. split("[,]", 0);


6 Answers

.NET 2.0 does not support LINQ - SO thread;
But you can create a 3.5 project in VS2005 - MSDN thread

Without lambda support, you'll need to do something like this:

string s = "a,b, b, c";
string[] values = s.Split(',');
for(int i = 0; i < values.Length; i++)
{
   values[i] = values[i].Trim();
}
like image 159
Jon G Avatar answered Oct 04 '22 12:10

Jon G


.NET 2.0 does not use lambda expressions. You need to compile to .NET 3.0 to use them.

like image 41
basher Avatar answered Oct 04 '22 14:10

basher


A way to do this without Linq & Lambdas

string source = "a,b, b, c";
string[] items = source.Split(new char[] { ',', ' ' }, StringSplitOptions.RemoveEmptyEntries);
like image 21
Cybermaxs Avatar answered Oct 04 '22 13:10

Cybermaxs


Lamba expression aren't included in c# 2.0

maybe you could refert to this post here on SO

like image 28
Rémi Avatar answered Oct 04 '22 14:10

Rémi


Split a Textbox value separated by comma and count the total number of values in text and splitted values are shown in 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";
        }
    }
like image 29
Mahesh Kumar Rajput Avatar answered Oct 04 '22 12:10

Mahesh Kumar Rajput


You could use LINQBridge (MIT Licensed) to add support for lambda expressions to C# 2.0:

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.

like image 24
Ryan Weir Avatar answered Oct 04 '22 12:10

Ryan Weir