Question

I have this string:

   string var = "x1,x2,x3,x4";

I want to get x1, x2, x3, x4 separately in four different strings.

Was it helpful?

Solution

This is a basic split

string yourString = "x1,x2,x3,x4";
string[] parts = yourString.Split(',');

foreach(string s in parts)
    Console.WriteLine(s);

I renamed the original variable into yourString. While it seems that it is accepted in LinqPad as a valid identifier I think that it is very confusing using this name because is reserved

OTHER TIPS

Split it up.

var tokens = someString.Split(',');

Now you can access them by their index:

var firstWord = tokens[0];

String.Split

var foo = "x1,x2,x3,x4";
var result = foo.Split(',');

There's also the "opposite: String.Join

Console.WriteLine(String.Join('*', result));

Will output: x1*x2*x3*x3

To separate values with comma between them, you can use String.Split() function.

string[] words = var.Split(',');
foreach (string word in words)
{
    Console.WriteLine(word);
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top