IRC聊天命令解析(IRC Chat Command Parsing)

编程入门 行业动态 更新时间:2024-10-17 07:21:16
IRC聊天命令解析(IRC Chat Command Parsing)

固定我把这里的代码放在其他需要帮助解决自己问题的人身上(假设他们遇到了问题)。

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; }

所以我已经尝试了几个小时,我环顾四周,找不到任何与我想做的事情有关的事情。

我有一个文本文件,我正在读取名为“commands.txt”,我正在尝试解析文本。 这是内容:

!help Hello, current commands are: !help, !ver !ver Testing this

现在,如果我拉

string x1 = File.ReadAllLines("commands.txt").ToString(); string[] x2 = x1.Split(' '); string x3 = x2[0]; Console.WriteLine(x3);

我得到'索引超出数组的范围'。 我不知道我做错了什么。 我也试图使用'静态布尔'来调用命令存在,到目前为止我得到了

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,但返回命令本身

FIXED I'm putting the code here for anyone else who needs help with their own problem (assuming they had the problem I had.

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; }

So I've been trying for a few hours and I looked around and I can't find anything related to what I'm trying to do.

I have a text file I'm reading called "commands.txt" and I'm trying to parse the text. Here's the contents:

!help Hello, current commands are: !help, !ver !ver Testing this

Now if I pull

string x1 = File.ReadAllLines("commands.txt").ToString(); string[] x2 = x1.Split(' '); string x3 = x2[0]; Console.WriteLine(x3);

I get 'Index outside the bounds of the array'. I have no idea what I'm doing wrong. I'm also trying to use a 'static bool' to call if the command exists and so far I got

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

and that isn't working as well.

What is causing that exception?

EDIT: 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; }

Now I know that returns True, how do I get it not to return true, but return the command itself

最满意答案

ReadAllLines返回一个字符串数组,你使用ToString而不是获取行,你得到的字符串数组的类型名称不包含任何while-space,所以Split(' ')不会改变任何东西。如果你想要要读取所有文本,请使用ReadAllText方法。

string x1 = File.ReadAllText("commands.txt");

似乎所有命令都以! 所以你可以把所有命令都放到这样的数组中:

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); }

如果你想排除 ! 在开头添加.Select(x => x.TrimStart('!'))在Where之后。

ReadAllLines returns an array of strings,you are using ToString and instead of getting lines, you get the type name of the string array which doesn't contain any while-space so Split(' ') doesn't change anything.If you want to read all text, use ReadAllText method.

string x1 = File.ReadAllText("commands.txt");

It seems all your command begins with ! so you can get all commands into an array like this:

string[] commands = File.ReadAllText("commands.txt") .Split() .Where(x => x.StartsWith("!")) .Distinct() .ToArray();

Then your method will look like this:

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

If you want to exclude the ! at the beginning add .Select(x => x.TrimStart('!')) after Where.

更多推荐

本文发布于:2023-08-04 22:36:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1423379.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:命令   IRC   Chat   Parsing   Command

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!