quote:
Originally posted by CookieRevised
It also doesn't take in account other spacing characters than ascii 32 (spacebar), which can occur on the command line. To overcome all this use regular expressions.
If you aren't up to using regular expressions you can still use a combination of substr() and IndexOf().
Or easier, use the split() method but with the optional limit parameter:
stringObj.split(" ", 2). This will limit your array to 2 elements, and the second element will always be the parameter.
But also note that with any method you use (except for the regular expression method) you need to take in account additional leading and trailing spaces. Thus you also need to trim the parameter element before using it.
(and take in account the casing of letters too)
Cookie, I don't mean to criticise you, but the
stringObj.split(" ", 2) method won't work any better than just using
stringObj.split(" ").
The only difference is that with the optional
limit parameter, your array is limited to 2 elements, without using the rest of the string.
eg:
code:
var str = "The quick brown fox jumps over the lazy dog.";
return str.split(" ",2);
will return a 2-element array: "The,quick"
so a command like "/command parameter" (2 spaces in between) will become "/command," with an empty 2nd element.
I had to try this to see it, as it's not in the docs (the limit parameter is, but the fact that it leaves the rest of the string isn't) so even with reading the docs you don't always get where you want
My conclusion is that regular expressions are the best way to go (unfortunately I don't know how to use them yet), but that using
stringObj.toLowerCase().split(" ") is an acceptable second, which also takes casing of letters into account.
You could even use a loop to filter out the empty elements caused by extra spaces.
I totally agree with the rest of your post, as usual