Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Latest commit

 

History

History
History
28 lines (22 loc) · 778 Bytes

File metadata and controls

28 lines (22 loc) · 778 Bytes
Copy raw file
Download raw file
Edit and raw actions

Substrings

substring is used to take a part of a string. Syntax: substring(first_index,last_index).

var a = 'Hello world!';
document.write(a.substring(1,6));

The preceding code snippet gives 'ello ' . Note that the 'w' (index 6) is not part of this substring.

We could also do,

var a = 'Hello world!';
document.write(a.substring(2));

This gives the whole string from the character with index 2. 'llo world!'

##substr

There is also a method substr() that works slightly differently. Instead of the second number being an index number, it gives the number of characters.

var a = 'Hello world!';
document.write(a.substr(1,6));

starts at the character with index 1 ('e') and then gives 6 characters, so the output is ello w

Morty Proxy This is a proxified and sanitized view of the page, visit original site.