1

When I run my code through the CMD, the message gets sent into:

void Main(string[]args) 

As everybody does if they use C# on visual studio. But when you run your code from CMD and type a message. it appears the argument can take in a message such as:

  • Are you asking about how to take the same input from any source (such as a text box) and parse it the same way that command line arguments are parsed into the `args` parameter? – madreflection Mar 15 '22 at 18:30
  • Windows removes the double quotes before passing the quoted string as one argument to the programs entry point. If you want to pass double quotes as part of an argument you need a custom escape-logic to do so, where some other sequence of chars or a single char represents the double quote char. – lidqy Mar 15 '22 at 18:34
  • Reply to madreflection, yes. lidqy, I am aware of that except somehow the args at main takes anything sent inbetween quotation marks as one argument. which is what I am struggling to solve. – randomUser1212421 Mar 15 '22 at 18:39
  • Well, here on Stack Overflow, you're expected to try it first, and then when you run into difficulty with what you've tried, you ask a question that includes the code that you've written. – madreflection Mar 15 '22 at 19:01
  • Every programmer should learn how to examine a string, character by character, to parse some piece of information from it. Courses generally have you learning sorting algorithms, but it seems that string processing algorithms have fallen off most syllabi. – madreflection Mar 15 '22 at 19:07
  • Before you judge, I have tried and the code is edited above. – randomUser1212421 Mar 15 '22 at 19:19

1 Answers1

3

You can treat quoted text as one string without using the Split function, instead making use of Regex. Take the following snippet as an example:

// In your case just read from the textBox for input
string input = "cars \"testing string\"";

// This code will split the input string along spaces,
// while keeping quoted strings together
string[] tmp = Regex.Split(input, 
    "(?<=^[^\"]*(?:\"[^\"]*\"[^\"]*)*) (?=(?:[^\"]*\"[^\"]*\")*[^\"]*$)");

// Now must remove the quotes from the Regex'd string
string[] args = tmp.Select(str => str.Replace("\"", "")).ToArray();

// Now when printing the arguments, they are correctly split
for (int i = 0; i < args.Length; i++)
{
    Console.WriteLine("args[" + i + "]:'" + args[i] + "'");
}

I found the particular regex string you needed at this link, here on stack overflow.

SteveOh
  • 96
  • 7
  • 1
    So I could use ' ' to take in a sentence into an argument? I basically send in multiple arguments but anything in between quotation marks is to remain altogether as further on I scan the other arguments for information. So if I use that I just need to figure a way of putting them into a replace command maybe? – randomUser1212421 Mar 15 '22 at 18:30
  • 1
    Wait, I get your point but I am trying to do this from my made user interface, which is what I am struggling with – randomUser1212421 Mar 15 '22 at 18:52
  • @robocyborg I updated my answer with a method that should work with your user interface – SteveOh Mar 15 '22 at 20:46