简体   繁体   English

C#-在同一行上读取2个字符串,在控制台应用程序上用空格隔开

[英]C# - Read 2 strings on the same line separated by whitespace on a console application

I'm doing a simple file manager that has to create, modify, delete and rename files on console. 我正在做一个简单的文件管理器,它必须在控制台上创建,修改,删除和重命名文件。 The idea is to make it so that the user inputs something like: "create file.txt" and the file will be created. 这样做的目的是使用户输入类似“ create file.txt”的内容,然后将创建文件。 So far I only have it to where it will ask for the file name AFTER typing the command, but I don't want it like that. 到目前为止,我只在输入命令后将其放在要求输入文件名的位置,但是我不希望这样。 Any ideas? 有任何想法吗?

Console.Write("What do you want to do?: "); //Carlos, si lees esto en tu busqueda por copiones, este codigo es mio. Saludos - Carlos Martinez.
        string line = Console.ReadLine();
        if (line == "exit") // Check String
        {
            Environment.Exit(0); 
        }
        if (line == "create")
        {
            Create(args); //Go to create
        }
        else
            Console.WriteLine("command does not exist\n");
        Main(args); 
    }

    static void Create(string[] args)
    {
        Console.Write("File name: ");
        string name = Console.ReadLine(); // Agarrar string
        using (StreamWriter writer =
    new StreamWriter(name, true))
            writer.Close();
        {
            Console.WriteLine("File created\n");
            Main(args); //Regresa
        }
    }

When you type a line and hit Enter , the result of Console.ReadLine will be something exactly what you typed, ie create test.txt . 当您键入一行并按EnterConsole.ReadLine的结果将与您键入的内容完全相同,即create test.txt You can split the string on space: 您可以在空间上分割字符串:

string line = Console.ReadLine();
string split = line.Split(new [] {' '});
string command = split[0];
string name = split[1];

if (line == "create")
{
    Create(name);
}

The Create method should also be updated to accept a single string parameter: 还应该更新Create方法以接受单个string参数:

static void Create(string fileName)

Note that the above doesn't handle input very well: 请注意,上面的方法不能很好地处理输入:

  • It fails if you only enter a command, ie create 如果仅输入命令,即create ,则失败
  • It fails if you enter a file name with a space ie create C:\\Users\\David Zych\\test.txt 如果输入带有空格的文件名,则失败,即create C:\\Users\\David Zych\\test.txt

If I understand your requirements correctly, then you can resolve the problem using string.StartsWith 如果我正确理解您的要求,那么可以使用string.StartsWith解决问题。

    Console.Write("What do you want to do?: ");
    string line = Console.ReadLine();
    if (line.StartsWith("exit"))
    {
        Environment.Exit(0); 
    }
    // Notice the space after the create word
    if (line.StartsWith("create ")) 
    {
        Create(line.Substring(6)); 
    }
    else
        Console.WriteLine("command does not exist\n");

of course the Create method should be changed to get just one string, the filename 当然,应将Create方法更改为仅获取一个字符串,即文件名

static void Create(string fileName)
{
    if(fileName.Length > 0)
    {
        Console.Write("File name: ");
        using (StreamWriter writer = new StreamWriter(fileName, true))
           ;
        Console.WriteLine("File created");
    }
    else
        Console.WriteLine("No filename given");
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM