C# 聊天命令解析

C# 聊天命令解析,c#,string,C#,String,固定的 我把代码放在这里是为了任何其他需要帮助解决自己问题的人,假设他们遇到了我遇到的问题 FIXED CODE THAT WORKS public static bool CommandExists(String value) { string[] commands = File.ReadAllText("commands.txt") .Split() .Where(x

固定的 我把代码放在这里是为了任何其他需要帮助解决自己问题的人,假设他们遇到了我遇到的问题

FIXED CODE THAT WORKS

    public static bool CommandExists(String value)
    {
        string[] commands = File.ReadAllText("commands.txt")
                       .Split()
                       .Where(x => x.StartsWith(value))
                       .Distinct()
                       .ToArray();
        return commands.Contains(value);
    }
    public static string CommandParse(String value, string nick)
    {
        IEnumerable<string> lines;
        lines = File.ReadLines("commands.txt");
        IEnumerable<string> command = lines
            .Where(d => d.StartsWith(value, 
                StringComparison.CurrentCultureIgnoreCase));
        foreach (string line in command) {
            string vals = line
                .Replace("@nick", nick)
                .Replace("@upnick", nick.ToUpper())
                .Replace(value + " ", "");
            return vals;
        }
        return null;
    }
我得到“数组边界外的索引”。我不知道我做错了什么。我还试图使用一个“静态bool”来调用这个命令是否存在,以及到目前为止我得到的结果

public static bool CommandExists(String value)
{
    if (File.ReadAllLines("commands.txt").Contains(value.ToString())) {
        return true;
    }
    else
    { 
        return false; 
    }
}
这也不管用

是什么导致了这种异常

编辑:CommandParse

    public static string CommandParse(string value, string nick)
    {
        string[] commands = File.ReadAllText("commands.txt")
               .Split()
               .Where(x => x.StartsWith("!"+value.ToLower()))
               .Distinct()
               .ToArray();
        string cmd = commands[1]
            .Replace("@nick", nick)
            .Replace("@nickup", nick.ToUpper());
        return cmd;
    }
现在我知道返回True,我如何让它不返回True,而是返回命令本身

ReadAllLines返回一个字符串数组,您使用的是ToString,而不是获取行,您得到的字符串数组的类型名不包含任何空格,因此拆分“”不会更改任何内容。如果要读取所有文本,使用ReadAllText方法

看来你的命令都是从开始的!因此,您可以将所有命令放入如下数组:

string[] commands = File.ReadAllText("commands.txt")
                   .Split()
                   .Where(x => x.StartsWith("!"))
                   .Distinct()
                   .ToArray();
public static bool CommandExists(String value)
{
    string[] commands = File.ReadAllText("commands.txt")
                   .Split()
                   .Where(x => x.StartsWith("!"))
                   .Distinct()
                   .ToArray();

    return commands.Contains(value);
}
然后您的方法将如下所示:

string[] commands = File.ReadAllText("commands.txt")
                   .Split()
                   .Where(x => x.StartsWith("!"))
                   .Distinct()
                   .ToArray();
public static bool CommandExists(String value)
{
    string[] commands = File.ReadAllText("commands.txt")
                   .Split()
                   .Where(x => x.StartsWith("!"))
                   .Distinct()
                   .ToArray();

    return commands.Contains(value);
}
如果你想排除的话!在开始处添加。选择x=>x.TrimStart'!'在Where之后。

ReadAllLines返回一个字符串数组,您使用的是ToString,而不是获取行,而是获取字符串数组的类型名称,该数组不包含任何while空格,因此拆分“”不会更改任何内容。如果要读取所有文本,请使用ReadAllText方法

看来你的命令都是从开始的!因此,您可以将所有命令放入如下数组:

string[] commands = File.ReadAllText("commands.txt")
                   .Split()
                   .Where(x => x.StartsWith("!"))
                   .Distinct()
                   .ToArray();
public static bool CommandExists(String value)
{
    string[] commands = File.ReadAllText("commands.txt")
                   .Split()
                   .Where(x => x.StartsWith("!"))
                   .Distinct()
                   .ToArray();

    return commands.Contains(value);
}
然后您的方法将如下所示:

string[] commands = File.ReadAllText("commands.txt")
                   .Split()
                   .Where(x => x.StartsWith("!"))
                   .Distinct()
                   .ToArray();
public static bool CommandExists(String value)
{
    string[] commands = File.ReadAllText("commands.txt")
                   .Split()
                   .Where(x => x.StartsWith("!"))
                   .Distinct()
                   .ToArray();

    return commands.Contains(value);
}

如果你想排除的话!在开始处添加。选择x=>x.TrimStart'!'在Where之后。

与论坛网站不同,我们不使用感谢、感谢的帮助或签名。请看。与论坛网站不同,我们不使用感谢、感谢的帮助或签名。看,现在要分析这个命令,这是个棘手的部分,不是吗?我该如何使用静态字符串命令parseString value{}来解析这个命令,这是很棘手的部分,不是吗?我将如何使用静态字符串命令ParseString值{}