String Function - substring

how to get a portion of the string in javascript?

Explanation

Object: String
Method or Function: substring(integer, integer)
Description: Takes two integer arguments. The first argument is the start index and the second argument is the end index. This method returns a part or portion of the main string beginning from begin index to last index.

Example Code:
<script language=javascript>
var ss = " string test ";
var result = ss.substring(3,6);
document.write(result);
</script>

Result:



Explanation:
In the above example,
  • the main string is stored in a variable ss.
  • the method substring takes two arguments 3 and 6.
  • this method takes a portion of the string from the position 3 up to 6 and forms a new string. i.e "rin"
  • the result is of variable type string.


Ask Questions

Ask Question