substring syntax in javascript

D

dennishancy

Trying to debug someone else's javascript code. There is a line that
looks like this:

req = req.substring(req.length-2, req.length);
From what I can gather in their comments, they want to capture the last
two characters of the string "req".

I'm guessing that the above line won't do that, and they want to change
it to

req = req.substring(req.length-1, 2);

In other words, the parameters are starting position, and length. Is
that correct?


Dennis Hancy
Eaton Corporation
Cleveland, OH
 
L

Lee

(e-mail address removed) said:
Trying to debug someone else's javascript code. There is a line that
looks like this:

req = req.substring(req.length-2, req.length);

two characters of the string "req".

I'm guessing that the above line won't do that, and they want to change
it to

req = req.substring(req.length-1, 2);

In other words, the parameters are starting position, and length. Is
that correct?

No:

substring(indexA, indexB)

substring extracts characters from indexA up to
but not including indexB.
 
A

ASM

Trying to debug someone else's javascript code. There is a line that
looks like this:

req = req.substring(req.length-2, req.length);

two characters of the string "req".

I think it's what that do
req = req.substring(req.length-1, 2);

In other words, the parameters are starting position, and length. Is
that correct?

Exactly not !

foo.substring(start,end)
or, in this case :
foo.substring(start);
 
M

Mick White

Trying to debug someone else's javascript code. There is a line that
looks like this:

req = req.substring(req.length-2, req.length);

two characters of the string "req".

I'm guessing that the above line won't do that, and they want to change
it to

req = req.substring(req.length-1, 2);

In other words, the parameters are starting position, and length. Is
that correct?
No!

function lastNLetters(str,N){
return str.substring(str.length-N);
}
alert(lastNLetters("abcde",3));


Mick
 
J

Jc

req = req.substring(req.length-1, 2);

In other words, the parameters are starting position, and length. Is
that correct?

FYI: This could have been easily looked up in online/offline javascript
documentation. The group FAQ has links to online documentation.

comp.lang.javascript FAQ - http://www.jibbering.com/faq/

You are likely thinking of substr, whose second parameter is length.
 
T

Thomas 'PointedEars' Lahn

Trying to debug someone else's javascript code. There is a line that
looks like this:

req = req.substring(req.length-2, req.length);
From what I can gather in their comments, they want to capture the last
^^
two characters of the string "req".

I'm guessing that the above line won't do that, [...]

But it does. RTFM, RTFFAQ and learn how to post.


PointedEars
 
J

Jeff

Use the MS Script Editor included free with MS Office 2002 and above,
for debugging Internet Explorer (IE).

This subject is of great interest to many JS developers, as there is no
obvious, low cost way to do sophisticated debugging in
IE6 other than to use the debugger described below, which is horribly
documented otherwise. I feel debugging is an important aspect of
projecting the useability of the language and needs to be made more
clear for new users.


Jeff Papineau
(e-mail address removed)


<FAQENTRY>

This is a page that describes how to install and use the MS Script
Editor to debug Javascript in Internet Explorer ( IE ). It has a
powerful debugger built into it that works really well for developers
supporting IE5+. This debugger/editor included with most versions of
Microsoft Office.

http://www.mandala.com/javascript/debug_javascript.html

..NET programmers may have better tools (VStudio) but this comes in
really handy for anyone developing with JSP and PHP and other dynamic
scripting languages which embed javascript, as well as any HTML page
using Javascript in Internet Explorer that needs a (almost) free
debugging environment.

</FAQENTRY>
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,181
Messages
2,570,969
Members
47,536
Latest member
VeldaYoung

Latest Threads

Top