I have this string:
string var = "x1,x2,x3,x4";
I want to get x1, x2, x3, x4 separately in four different strings.
I have this string:
string var = "x1,x2,x3,x4";
I want to get x1, x2, x3, x4 separately in four different strings.
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
Split it up.
var tokens = someString.Split(',');
Now you can access them by their index:
var firstWord = tokens[0];
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);
}