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.Split(' ')[1]
is just the first word after the space, any ideas on completely dividing and getting all words after the first space?
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
(I know there are several answers to this question, but none written in C# from where I searched.)
We used the str. strip() method to remove any leading or trailing spaces from the string before calling the split() method. If you need to split a string only on the first whitespace character, don't provide a value for the separator argument when calling the str. split() method.
You can split a String by whitespaces or tabs in Java by using the split() method of java. lang. String class. This method accepts a regular expression and you can pass a regex matching with whitespace to split the String where words are separated by spaces.
To split a string on newlines, you can use the regular expression '\r?\ n|\r' which splits on all three '\r\n' , '\r' , and '\n' . A better solution is to use the linebreak matcher \R which matches with any Unicode linebreak sequence. You can also split a string on the system-dependent line separator string.
Use the overload of s.Split that has a "maximum" parameter.
It's this one: 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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With