3

what are the characters used to separate command-line arguments (such as white space, \t)? How do you check whether a string contains a separator? for example: Check("abcd") is false and Check("ab cd") or Check("ab\tcd") is true

Louis Rhys
  • 34,517
  • 56
  • 153
  • 221
  • 2
    See [Split string containing command-line parameters into string array in C#](http://stackoverflow.com/questions/298830/split-string-containing-command-line-parameters-into-string-in-c). It has both a managed solution and instructions for calling the Windows `CommandLineToArgvW` function. – Matthew Flaschen Sep 08 '10 at 04:20

1 Answers1

3

C# by default splits your arguments on bases of white space so there shouldn't be a need to split your arguments.

But if you have to do it for some reason then

You can split you command line arguments using string.split(' ') and get the array of strings

so basically you will do something like this

bool Check(string argument)
{
    string[] arguments = argument.split(' ');
    if (arguments.Length > 1) // In your case if you are expecting 2 or more arguments
    {
        return true;
    }
    return false;
}
Ankit
  • 653
  • 2
  • 8
  • 18
  • C# by default uses whitespace as the separator. if you want to use some other separators then you can take the argument and split it using the separators that you would like to support. so if you want to separate it using '-' then u can use argument.split('-') – Ankit Sep 08 '10 at 04:25
  • 1
    I mean, in command line if you pass "a\tb" a and b would become separated, right? I am asking what characters (other than whitespace) count as separators in command line – Louis Rhys Sep 08 '10 at 04:34
  • if you pass "a\tb" from a console it will be treated as string characters instead of escape sequence. so the parameter that you will receive in your application will be "a\\tb". – Ankit Sep 09 '10 at 05:00
  • 1
    @LouisRhys a tab would be treated as white space (i.e. if you can tab on the command prompt)...also you could always do a simple `foreach(var arg in args) Console.WriteLine(arg);` to test what the default behaviour is – aateeque Nov 07 '11 at 16:13
  • I just came by this answer and I had to mention `return argument.split(' ') > 1;` – Bob Vandevliet Jul 14 '21 at 14:02