quote:
Originally posted by WaterRatj
I'm trying to split something
var sPrefix = sMessage.split(" ");
var sPrefixTheRest = ???
Lets say the string is !nick Hello Kitty.
sPrefix[0] will be !nick
But i' trying to the a string now with all the rest of the line without the !nick, can anyone tell me hows the best way to do this?
split() is usually only good if you want to turn something into an array or if you are absolutely certain that a certain string includes the exact amount of delimiters and items you want. Now, it can be done with the very slow split() function though, but you will need a lot of stuff and trickery and in the end it would still not be fail-proof.
As a first simple alternative:
code:
var mystring = "!nick Hello Kitty";
// Search the first space in the string
var X = mystring.indexOf(" ");
// Increase the index by one (we don't want the space itself)
X = X + 1;
// Get everything after the space, thus starting from the character at position X till the end
mystring = mystring.substr( X );
combined into one line:
code:
mystring = mystring.substr(mystring.indexOf(" ") + 1);
But if that string is something entered by the user (like a command):
Then what if the user forget to type anything behind "!nick"?
And what if the user forget the space after "!nick"?
And what if the user has typed spaces before "!nick"?
And what if the user has typed multiple spaces after "!nick"?
etc...
All these things need to be taken care of.
If you want to seperate the parameters from the command in an OnEvent_ChatWndSendMessage function, you better use regular expressions. This is the most accurate and versitile method to use for this. For an explaining example see
CookieRevised's reply to Gettin data from "/" commands.
PS: do not use "!" as prefix for normal commands. A normal local command should always have "/" as prefix. Most likely, Your script isn't the only script the user is using. Using something different for the prefix than the usual prefix may confuse the user and it will give problems with some other scripts also, etc.