Advertisement
Help Keep Boards Alive. Support us by going ad free today. See here: https://subscriptions.boards.ie/.
If we do not hit our goal we will be forced to close the site.

Current status: https://keepboardsalive.com/

Annual subs are best for most impact. If you are still undecided on going Ad Free - you can also donate using the Paypal Donate option. All contribution helps. Thank you.
https://www.boards.ie/group/1878-subscribers-forum

Private Group for paid up members of Boards.ie. Join the club.

C# string manipulation

  • 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, Registered Users 2, Paid Member Posts: 14,174 ✭✭✭✭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, Registered Users 2 Posts: 15,079 ✭✭✭✭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