offline version v3


64 of 264 menu

substring method

The substring method returns a substring from a string (the original string is not changed). The first parameter specifies the number of the character where the method starts cutting (numbering starts from zero), and the second parameter is the number of the character where the cutting should end (the character with this number is not included in the cut substring). The second parameter is optional, if it is not specified, then all characters up to the end of the string will be cut.

Syntax

string.substring(where to start, [where to stop]);

Example

Let a string be given. Let's cut out the characters from the first to the third:

let str = 'abcde'; let sub = str.substring(1, 3); console.log(sub);

The code execution result (character numbered 3 will not be included in the substring):

'bc'

Example

Let's now cut out the characters from 1st to the end of a string (for this, we will not set the second parameter of the method):

let str = 'abcde'; let sub = str.substring(1); console.log(sub);

The code execution result:

'bcde'

See also

  • the substr and slice methods
    that also return a substring
enru