substring()  String  instance method getterS2C Home  « Globals « String « substring()

Description

Returns a part of a string defined by parameters.

Syntax

Signature Description
aString.substring(index1[, index2])Returns a part of a string defined by parameters.

Parameters

Parameter Description
index1The starting extraction position of the zero-based character index between 0 and string length -1.
  • Positive values greater than length of string return an empty string.
  • Negative values or NaN are changed to 0.
  • Negative values greater than length of string start extraction from 0.
index2The ending extraction position of the zero-based character index between 0 and string length.
  • Optional and defaults to string length.
  • Returns characters up to but excludes index2.
  • Negative values or NaN are changed to 0.
  • If index1 is equal to index2 an empty string is returned.
  • Parameter values greater than string length are processed as string length.
  • If index2 is greater than index1 their positions are switched.

Examples

The code below returns part of a string.


// Create a string variable.
  var aSentence = 'I like to shine my shoes';

/*
 Extract part of the string.
 /*
alert(aSentence.substring(2, 4));


/*
 Extract another part of the string.
 /*
alert(aSentence.substring(6, 9));

Press the button below to action the above code:


Related Tutorials

JavaScript Basic Tutorials - Lesson 8 - Strings

go to home page Homepage go to top of page Top