So I've a string:
string x = "DR\nDC\nDD";
I want to get each line in a separated variable like this:
string y1 = "DR";
String y2 = "DC";
String y3 = "DD";
How can I do that?.
So I've a string:
string x = "DR\nDC\nDD";
I want to get each line in a separated variable like this:
string y1 = "DR";
String y2 = "DC";
String y3 = "DD";
How can I do that?.
Use String's split
method.
string[] values = x.Split("\n");
And then if you really want them in 3 separate variables... :
string y1 = values[0];
string y2 = values[1];
string y3 = values[2];
But I would suggest leaving it in an array and processing from there.
Use String.Split like this:
var arr = x.Split('\n');
string y1 = arr[0];
string y2 = arr[1];
string y3 = arr[2];
foreach (string yN in x.Split('\n'))
{
// do something with yN
}
or
string[] yN = x.Split('\n');
string y1 = yN[0];
string y2 = yN[1];
string y3 = yN[2];