User Tools

Site Tools


javascript:substring

JavaScript - SubString

Extract starting at position 2, and extract the rest of the string:

var str = "Hello world!";
var res = str.substring(2);

Result:

llo world!

If “start” is greater than “end”, it will swap the two arguments:

var str = "Hello world!";
var res = str.substring(4, 1);

Result:

ell

If “start” is less than 0, it will start extraction from index position 0:

var str = "Hello world!";
var res = str.substring(-3);

Result:

Hello world!

Extract only the first character:

var str = "Hello world!";
var res = str.substring(0, 1);

Result:

H

Extract only the last character:

var str = "Hello world!";
var res = str.substring(11, 12);

Result:

!
javascript/substring.txt · Last modified: 2020/07/15 10:30 by 127.0.0.1

Donate Powered by PHP Valid HTML5 Valid CSS Driven by DokuWiki