ND's
ND's

Reputation: 2215

split string with string and retrieve value in c#

string mystring = "-p \"C:\\Users\\mplususer\\Documents\\SharpDevelop Projects\\test\\test\\bin\\Debug\\test.exe\" -t False -r TestRun"

i have to extract from mystring whatever in front of command -r here is "Testrun"

string[] value = mystring .Split(new string[] { "\"-r " }, StringSplitOptions.None);
                if (value.Length != 0)
                {
                    runConfig = value[1].Replace("\"", "");
                }

Upvotes: 0

Views: 56

Answers (1)

Tim Schmelter
Tim Schmelter

Reputation: 460158

I'd use this String.Split version:

string[] value = mystring.Split(new string[] {" -r " }, 2, StringSplitOptions.None);
string runConfig = value.Last().Split()[0]; 

The second Split is just for the case that there are other parameters after the -r possible.

Upvotes: 2

Related Questions