Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

C# string manipulation

Options
  • 03-05-2008 12:14am
    #1
    Closed Accounts Posts: 1,444 ✭✭✭


    Hi guys,

    If anyone could share some tips, I'd be most grateful.

    I have a string "command('arg1','arg2','arg3',....,'argn')"

    I need to convert this to:
    string command="command";
    string[] args= {"arg1","arg2","arg3",....."argn"}
    

    I'm trying to use the split functions but they're driving me nuts!

    Also, I'm in a C# windows GUI application so I can't write to the console which is making debugging a nightmare! I can't write to a label cos I'm inside a class that doesn't have simple access to the GUI elements.

    Aggh. I only rarely have to use C#, but each time I do, I wish I was back in the land of Perl.


Comments

  • Registered Users Posts: 14,148 ✭✭✭✭Lemming


    Cantab. wrote: »
    Hi guys,

    If anyone could share some tips, I'd be most grateful.

    I have a string "command('arg1','arg2','arg3',....,'argn')"

    I need to convert this to:
    string command="command";
    string[] args= {"arg1","arg2","arg3",....."argn"}
    

    I'm trying to use the split functions but they're driving me nuts!

    Also, I'm in a C# windows GUI application so I can't write to the console which is making debugging a nightmare! I can't write to a label cos I'm inside a class that doesn't have simple access to the GUI elements.

    Aggh. I only rarely have to use C#, but each time I do, I wish I was back in the land of Perl.

    I don't understand why you can't debug. What's what a debug session is for. You can insert break-points and inspect (and indeed edit) the values of variables. C# is remarkably easy to debug. A great deal easier to debug than Perl. By a wide, wide country mile.

    Regardless (and bear with me this code hasn't been written in VS so apologies for any mistakes): you want to find the index value of the first '(' and get the args substring from there, and then split that into a char[] using the string.split command. There's probably a more elegant way of doing this, but it's late and my brain has gone bye-byes.
    String original = "command('args1', 'args2', 'args3')"
    
    String args = original.Substring(original.IndexOf('('));
    
    String argsArray[] = args.Split(',');
    

    The rest should be easy enough to work out around that.

    Here's the MSDN description of all the available members (properties/methods/constructors/etc) for the .Net String class: Clicky

    [edit. that above link is for .Net framework 3.5 which has added a couple of extra bits and bobs, so be mindful of that. There are framework specific pages linked at the above right of the page]


  • Closed Accounts Posts: 1,444 ✭✭✭Cantab.


    Hi Lemming,

    Thanks for the input.

    In the end, the following achieved what I wanted to do:
    message="cmd(arg1,arg2)";
    
    string command;
    string[] args;
    
    message = message.TrimEnd(new char[] { ')' });
    string[] split1 = message.Split(new char[] { '(' });
    string[] split2 = split1[1].Split(new char[] { ',' });
    
    command = split1[0];
    args = split2;
    

    I guess unfamiliarity with the environment can make a seemingly simple parsing problem infuriating!

    I find access to the Microsoft online documentation annoyingly slow. Anyone got any better ideas?


  • Registered Users Posts: 15,065 ✭✭✭✭Malice


    Cantab. wrote: »
    I find access to the Microsoft online documentation annoyingly slow. Anyone got any better ideas?
    MSDN is a free download although it's a slightly older version (April 2007) and weighs in at a hefty 2.19Gb for the whole thing.

    With regard to your debugging problem, I have the opposite issue, anytime I have to move away from Visual Studio e.g. to PHP or Classic ASP or whatever, I find myself really missing the debugging features of the VS.NET IDE!


Advertisement