对于聊天机器人,如果有人说“!say”,它会在空格后背诵你说的话。简单。
示例输入:
!say this is a test
期望的输出:
this is a test
为了论证,字符串可以表示为s
。 s.Split(' ')
产生一个数组。
s.Split(' ')[1]
只是空格后的第一个单词,关于完全划分并得到第一个空格后的 所有 个单词有什么想法吗?
我试过类似的方法:
s.Split(' ');
for (int i = 0; i > s.Length; i++)
{
if (s[i] == "!say")
{
s[i] = "";
}
}
输入是:
!say this is a test
输出:
!say
这显然不是我想要的:p
(我知道这个问题有几个答案,但在我搜索的地方没有一个是用 C# 写的。)
最佳答案
使用具有“最大”参数的 s.Split 的重载。
是这个: http://msdn.microsoft.com/en-us/library/c1bs0eda.aspx
看起来像:
var s = "!say this is a test";
var commands = s.Split (' ', 2);
var command = commands[0]; // !say
var text = commands[1]; // this is a test
关于c# - 在第一个空格处划分字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9366690/