For a chat-bot, if someone says "!say " it will recite what you say after the space. Simple.
对于聊天机器人,如果有人说“!说”,它会在空间后背诵你所说的内容。简单。
Example input:
示例输入:
!say this is a test
Desired output:
期望的输出:
this is a test
The string can be represented as s
for sake of argument. s.Split(' ')
yields an array.
为了论证,字符串可以表示为s。 s.Split('')产生一个数组。
s.Split(' ')[1]
is just the first word after the space, any ideas on completely dividing and getting all words after the first space?
s.Split('')[1]只是空间之后的第一个单词,是否有关于在第一个空格之后完全划分和获取所有单词的任何想法?
I've tried something along the lines of this:
我尝试过这样的事情:
s.Split(' ');
for (int i = 0; i > s.Length; i++)
{
if (s[i] == "!say")
{
s[i] = "";
}
}
The input being:
输入是:
!say this is a test
The output:
输出:
!say
Which is obviously not what I wanted :p
这显然不是我想要的:p
(I know there are several answers to this question, but none written in C# from where I searched.)
(我知道这个问题有几个答案,但没有一个用C#从我搜索的地方写的。)
4 个解决方案
#1
30
Use the overload of s.Split that has a "maximum" parameter.
使用具有“最大”参数的s.Split的重载。
It's this one: http://msdn.microsoft.com/en-us/library/c1bs0eda.aspx
就是这个:http://msdn.microsoft.com/en-us/library/c1bs0eda.aspx
Looks like:
好像:
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
#2
7
You can use string.Substring method for that:
您可以使用string.Substring方法:
s.Substring(s.IndexOf(' '))
#3
2
var value = "say this is a test";
return value.Substring(value.IndexOf(' ') + 1);
#4
0
This code is working for me. I added the new [] and it works
这段代码对我有用。我添加了新的[]并且它有效
var s = "!say this is a test";
var commands = s.Split (new [] {' '}, 2);
var command = commands[0]; // !say
var text = commands[1]; // this is a test
#1
30
Use the overload of s.Split that has a "maximum" parameter.
使用具有“最大”参数的s.Split的重载。
It's this one: http://msdn.microsoft.com/en-us/library/c1bs0eda.aspx
就是这个:http://msdn.microsoft.com/en-us/library/c1bs0eda.aspx
Looks like:
好像:
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
#2
7
You can use string.Substring method for that:
您可以使用string.Substring方法:
s.Substring(s.IndexOf(' '))
#3
2
var value = "say this is a test";
return value.Substring(value.IndexOf(' ') + 1);
#4
0
This code is working for me. I added the new [] and it works
这段代码对我有用。我添加了新的[]并且它有效
var s = "!say this is a test";
var commands = s.Split (new [] {' '}, 2);
var command = commands[0]; // !say
var text = commands[1]; // this is a test