What happened to the Messenger Plus! forums on msghelp.net?
Shoutbox » MsgHelp Archive » Messenger Plus! for Live Messenger » Scripting » Question :)

Question :)
Author: Message:
CookieRevised
Elite Member
*****

Avatar

Posts: 15517
Reputation: 173
– / Male / Flag
Joined: Jul 2003
Status: Away
RE: Question :)
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.

;)

This post was edited on 03-28-2008 at 12:26 AM by CookieRevised.
.-= A 'frrrrrrrituurrr' for Wacky =-.
03-28-2008 12:22 AM
Profile PM Find Quote Report
« Next Oldest Return to Top Next Newest »

Messages In This Thread
Question :) - by WaterRatj on 03-27-2008 at 03:55 PM
RE: Question :) - by Quantum on 03-27-2008 at 04:27 PM
RE: Question :) - by MeEtc on 03-27-2008 at 04:43 PM
RE: Question :) - by WaterRatj on 03-27-2008 at 05:02 PM
RE: Question :) - by CookieRevised on 03-28-2008 at 12:22 AM
RE: Question :) - by WaterRatj on 03-28-2008 at 12:31 AM


Threaded Mode | Linear Mode
View a Printable Version
Send this Thread to a Friend
Subscribe | Add to Favorites
Rate This Thread:

Forum Jump:

Forum Rules:
You cannot post new threads
You cannot post replies
You cannot post attachments
You can edit your posts
HTML is Off
myCode is On
Smilies are On
[img] Code is On