What happened to the Messenger Plus! forums on msghelp.net?
Shoutbox » MsgHelp Archive » Messenger Plus! for Live Messenger » Scripting » startsWith() and endsWith()

startsWith() and endsWith()
Author: Message:
Flippy
Junior Member
**


Posts: 29
34 / Male / Flag
Joined: Jun 2009
O.P. startsWith() and endsWith()
Hey,

I'm looking for a function that will tell me if a string starts or ends with a certain string. In any language I've worked in so far you had the StartsWith() and EndsWith() functions, but they don't seem to work in jscript.

I've looked on google, but I could only find examples for Javascript, for example this:
http://www.tek-tips.com/faqs.cfm?fid=6620

So I decided just to use that in jscript, but it won't work.


The first try, I made it into this:
(Note that I didn't try to use that 'prototyping' thing, don't need that):
JScript code:
function startsWithToken(orig, str)
{
    return (orig.match("^"+str)==str)
}
function endsWithToken(orig, str)
{
    return (orig.match(str+"$")==str)
}

But it didn't work. They are returning false while they should be true.

I figured maybe the regex works differently in jscript, and I was stuck there... I didn't know how to convert it to jscript properly.

So I decided to just make it work for my purpose only, which is to see if the orig string starts or ends with the string "$$".

So I made it into this:
JScript code:
function startsWithToken(orig)
{
    return (orig.match(/^\$\$/) == "$$")
}
function endsWithToken(orig)
{
    return (orig.match(/^\$\$$/) == "$$")
}


Now, the startsWith function works fine, but the endsWith method does not...

I suppose my regex is faulty, especially with the many $ signs lol... The last $ sign appears to mean something like "at the end" (I guess, I'm really rusty at regex), but I also need it to check the string for actual $ characters...

Can anyone help me out? This shouldn't be too hard, but unfortunately jscript is a little... well :$ let's just say I don't like it too much hehe.
06-17-2009 04:59 PM
Profile E-Mail PM Find Quote Report
Mnjul
forum super mod
******

Avatar
plz wub me

Posts: 5396
Reputation: 58
– / Other / Flag
Joined: Nov 2002
Status: Away
RE: startsWith() and endsWith()
Maybe this will work..

JScript code:
function startsWithToken(orig, str)
{
    return orig.indexOf(str)===0;
}
function endsWithToken(orig, str)
{
    return orig.lastIndexOf(str)===(orig.length-str.length);
}


I'm assuming str won't be longer than orig. If str's length is exactly one char more than orig's length, then orig.length-str.length returns -1 which evaluates the expression to true.

This post was edited on 06-17-2009 at 05:31 PM by Mnjul.
06-17-2009 05:28 PM
Profile PM Web Find Quote Report
Flippy
Junior Member
**


Posts: 29
34 / Male / Flag
Joined: Jun 2009
O.P. RE: RE: startsWith() and endsWith()
quote:
Originally posted by Mnjul
Maybe this will work..

JScript code:
function startsWithToken(orig, str)
{
    return orig.indexOf(str)===0;
}
function endsWithToken(orig, str)
{
    return orig.lastIndexOf(str)===(orig.length-str.length);
}


I'm assuming str won't be longer than orig. If str's length is exactly one char more than orig's length, then orig.length-str.length returns -1 which evaluates the expression to true.

Thanks. I didn't try it yet (will do later tonight), but I cannot guarantee that orig is longer than str. str will actually be "$$" always (at least in my script), and orig is simply the message someone receives. That message might be a simple "k" or something like that, in which case this would fail (right?).

I suppose I can always use the indexOf function to see if the message contains any "$$" in the first place, before continuing and doing this. That would probably solve the problem.


One more thing... What the hell is a triple "=" ? How does this even work lol.
06-17-2009 06:26 PM
Profile E-Mail PM Find Quote Report
CookieRevised
Elite Member
*****

Avatar

Posts: 15519
Reputation: 173
– / Male / Flag
Joined: Jul 2003
Status: Away
RE: startsWith() and endsWith()
quote:
Originally posted by Mnjul
Maybe this will work..

JScript code:
function startsWithToken(orig, str)
{
    return orig.indexOf(str)===0;
}
function endsWithToken(orig, str)
{
    return orig.lastIndexOf(str)===(orig.length-str.length);
}

I'm assuming str won't be longer than orig. If str's length is exactly one char more than orig's length, then orig.length-str.length returns -1 which evaluates the expression to true.
Fixed by using:
Javascript code:
return (orig.lastIndexOf(str)===orig.length-str.length) && (orig.length>=str.length);


----------------------

For that triple '=', those are identity operators (opposed to a double '=' which is a equality operator). In almost all cases it is better to use identity operators instead of equality operators, unless there is a specific reason you need equality instead of identity.
see CookieRevised's reply to Script about lock messenger.

or look it up in the Windows Script Documentation > Index > Comparison operators > JScript5.6

This post was edited on 06-17-2009 at 07:31 PM by CookieRevised.
.-= A 'frrrrrrrituurrr' for Wacky =-.
06-17-2009 07:19 PM
Profile PM Find Quote Report
Flippy
Junior Member
**


Posts: 29
34 / Male / Flag
Joined: Jun 2009
O.P. RE: startsWith() and endsWith()
I see, so it has to do with type conversion.

That's one more thing I don't really understand about this language, types... Is it just me or is every variable of type 'var'? And you don't need to specify any type (int, string, etc) in function arguments either, or what kind of type your function returns.

I'm used to Object Oriented languages like VB.NET and C#.NET, where you need to declare each type explicitly. There, using anything such as "5" == 5 will simply error; you cannot compare apples to oranges (or strings to integers for that matter)...

Oh well, I don't think I'll ever be using jscript for anything else so I shouldn't whine :p
06-17-2009 07:46 PM
Profile E-Mail PM Find Quote Report
CookieRevised
Elite Member
*****

Avatar

Posts: 15519
Reputation: 173
– / Male / Flag
Joined: Jul 2003
Status: Away
RE: startsWith() and endsWith()
quote:
Originally posted by Flippy
I see, so it has to do with type conversion.

That's one more thing I don't really understand about this language, types... Is it just me or is every variable of type 'var'? And you don't need to specify any type (int, string, etc) in function arguments either, or what kind of type your function returns.
Correct. And this is the same in VBScript and JavaScript btw.
But even in C#, VB, etc you have that kind of type, usually it is called a Variant.

However, although JScript has a very loose type convention (which can be handy, but also prone to user errors), you can define variables as a specific type using the New operator. In fact, to be more correct, you actually define the variable as an object of a certain type. So your variable actually becomes an object. eg:
var myBoolean = new boolean();

However, the moment you start comparing normal variables, JScript internally converts the variables to the proper types. Hence the existance and difference between equality and identity operators.

In fact, you actually provided a nice example yourself to explain it. See below (the fine print):

-----------------------------

Anywyas, in regards to your reg. expression:
quote:
Originally posted by Flippy
JScript code:
function startsWithToken(orig)
{
    return (orig.match(/^\$\$/) == "$$")
}
function endsWithToken(orig)
{
    return (orig.match(/^\$\$$/) == "$$")
}

Now, the startsWith function works fine, but the endsWith method does not...
Because you actually check if the string is exactly "$$", nothing more, nothing less. This because you have the ^ in there (meaning 'from the beginning'). So, the correct expression is:
JScript code:
function endsWithToken(orig)
{
    return (orig.match(/\$\$$/) == "$$")
}

PS: in regards to the identity and equality issue. Here we use == instead of === because match actually returns an array, and you compare it to a string. So these are two different types.

If you would use === then the comparisson would always return false because the two things you compare are of a different type (internaly).

Using == makes that JScript internally first converts everything to a string (in this case). Thus it first converts the array to a string prior to comparing. When doing so, it simply takes the first array element disgarding all the rest and converts that into a string.


But better would be using the test method instead. Because that does not require the internal reg. exp. object to update, it doesn't need to create and populate an array, and already returns a boolean (so JScript doesn't need to converts types either). And thus it also runs a lot faster:
JScript code:
function startsWithToken(orig)
{
    return /^\$\$/.test(orig)
}
function endsWithToken(orig)
{
    return /\$\$$/.test(orig)
}


This post was edited on 06-17-2009 at 08:13 PM by CookieRevised.
.-= A 'frrrrrrrituurrr' for Wacky =-.
06-17-2009 07:52 PM
Profile PM Find Quote Report
markee
Veteran Member
*****

Avatar

Posts: 1621
Reputation: 50
35 / Male / Flag
Joined: Jan 2006
RE: startsWith() and endsWith()
I normally am a person who uses regular expressions all over the place (it is my job after all) but in this case I would highly recommend not doing it as it is a much slower option.  Instead I would use the following

JScript code:
function startsWithToken(orig)
{
    return (orig.charAt(0) == "$" && orig.charAt(1) == "$");
}
function endsWithToken(orig)
{
    var len = orig.length;
    return (orig.charAt(len-2) == "$" && orig.charAt(len-1) == "$");
}


I am not sure if a single character string is equivalent to a character, that is why I didn't use "===".
[Image: markee.png]
06-18-2009 07:46 AM
Profile PM Find Quote Report
CookieRevised
Elite Member
*****

Avatar

Posts: 15519
Reputation: 173
– / Male / Flag
Joined: Jul 2003
Status: Away
RE: startsWith() and endsWith()
quote:
Originally posted by markee
I am not sure if a single character string is equivalent to a character, that is why I didn't use "===".
yup, it is. Both are 'strings'.
.-= A 'frrrrrrrituurrr' for Wacky =-.
09-22-2009 08:15 AM
Profile PM Find Quote Report
brooooof
New Member
*


Posts: 2
Joined: Mar 2009
RE: startsWith() and endsWith()
Thanks (Y)
شات

This post was edited on 09-27-2009 at 06:46 AM by brooooof.
09-27-2009 06:43 AM
Profile E-Mail PM Find Quote Report
jerone
Junior Member
**

Avatar
i like it

Posts: 30
37 / Male / –
Joined: Jun 2006
RE: startsWith() and endsWith()
I don't know if this works in Jscript:
JScript code:
function startsWithToken(orig, str)
{
    return new RegExp("^"+str.replace(/[\|\(\)\$\^\*\+\[\]\?]/,"\$1"),"gi").test(orig);
}
function endsWithToken(orig, str)
{
    return new RegExp(str.replace(/[\|\(\)\$\^\*\+\[\]\?]/,"\$1")+"$","gi").test(orig);
}
console.log(startsWithToken("a$$","a"));  // returns true;
console.log(startsWithToken("$$a","a"));  // returns false;
console.log(endsWithToken("$$a","a"));  // returns true;
console.log(endsWithToken("a$$","a"));  // returns false;


This post was edited on 10-08-2009 at 11:42 AM by jerone.
[Image: Jerone.png]
10-08-2009 11:40 AM
Profile E-Mail PM Find Quote Report
« Next Oldest Return to Top Next Newest »


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