1

I am using Cosmos to make a simple OS to understand a bit about it. If I want to make a command line called echo that echos user's input, first I need to check if the input has "echo" in front of it. For example, if i input "echo hello world", I want my VMware to echo "hello world" because echo is my new command line.

What I tried is

String input = Console.ReadLine();
if (input.Contains("echo")) {
    Console.WriteLine(input} 
}

It's not efficient. First, VMware says

IndexOf(..., StringComparison) not fully supported yet!

And the user might type "echo" in the middle of his string, not as the command.

Are there any efficient ways to solve this?

Vivek Nuna
  • 25,472
  • 25
  • 109
  • 197
m.k
  • 73
  • 1
  • 10

3 Answers3

1
if(!string.IsNullOrEmpty(input) && input.StartsWith("echo"))
{
    Console.WriteLine(input);
}

You should use StartsWith instead of Contains. Better to check first whether a string is null or empty.

Vivek Nuna
  • 25,472
  • 25
  • 109
  • 197
  • with this code in my protected override void Run(), VMware quits automatically as soon as I input anything without looping. Do you know why? – m.k Oct 25 '16 at 19:39
0

You can split it using space, and check with the switch.

String input = Console.ReadLine();
String[] input_splited = input.split(' ');
switch(input_splited[0]){
    case 'echo':
      String value = input_splited[1];
      Console.WriteLine(value);
      break;
    case 'other_cmd':
      String other_value = input_splited[1];
      break;
}

I hope it works for u. :)

0

I figure out that you need something like that:

       const string command = "echo";
        var input = Console.ReadLine();

        if (input.IndexOf(command) != -1)
        {                
            var index = input.IndexOf("echo");               
            var newInputInit = input.Substring(0, index);
            var newInputEnd = input.Substring(index + command.Length);
            var newInput = newInputInit + newInputEnd;
            Console.WriteLine(newInput);
        }

        Console.ReadKey();