Substrings

JavaScript FAQ | JavaScript Strings and RegExp FAQ  

Question: How do I extract a substring from a string?

Answer: To extract a substring from a string, use the substring method:

string.substring(start,end)
Here
string  is the string from which you want to extract a substring.
start is the number specifying the position of the character at which the substring begins.
(The character at start itself will be included in the substring.)
end is the number specifying the position of the character at which the substring ends.
(The character at end will not be included in the substring.)

Note that the first character in the string corresponds to position 0, and the last character to position string.length-1.

Examples:

'Hello'.substring(0,2)  // 'He'
'Hello'.substring(0,4)  // 'Hell'
'Hello'.substring(1,3)  // 'el'

Copyright © 1999-2011, JavaScripter.net.