views:

212

answers:

4

I am having a command line arguments as like this I need to get the two as like this how is it possible

ApplicationId =1; Name =2

I like to get the two values 1,2 in single array how to do this.

A: 

C# Split String Examples

Dutow
+2  A: 

There's some good libraries mentioned on 631410 and 491595. I've personally used the WPF TestAPI Library mentioned by sixlettervariables and it is indeed pretty darn good

Dan F
+1  A: 

Try

string values = "ApplicationId =1; Name =2";
string[] pairs = values.Split(';');

string value1 = pairs[0].Split('=')[1];
string value2 = pairs[1].Split('=')[1];

You'll need better error checking of course, but value1 and value2 should be "1" and "2" respectively

Binary Worrier
+5  A: 

It isn't entirely clear to me, but I'm going to assume that the arguments are actually:

 ApplicationId=1 Name=2

the spacing etc is important due to how the system splits arguments. In a Main(string[] args) method, that will be an array length 2. You can process this, for example into a dictionary:

    static void Main(string[] args) {
        Dictionary<string, string> options = new Dictionary<string, string>();
        foreach (string arg in args)
        {
            string[] pieces = arg.Split('=');
            options[pieces[0]] = pieces.Length > 1 ? pieces[1] : "";
        }

        Console.WriteLine(options["Name"]); // access by key

        // get just the values
        string[] vals = new string[options.Count];
        options.Values.CopyTo(vals, 0);
    }
Marc Gravell
Yep, that's pretty much what I do for utilities that require named arguments.
Christian Hayter