sub string in jquery

Rashmi
Rashmi
1068 Points
19 Posts

Hi,

How to use sub string in jquery?

Views: 8953
Total Answered: 1
Total Marked As Answer: 1
Posted On: 20-Jun-2015 08:00

Share:   fb twitter linkedin
Answers
Rahul Maurya
Rahul M...
4918 Points
28 Posts
         

Hi Rashmi,

The substring() method extracts the characters from a string( between two specified indices), returns the new sub string.
This method extracts the characters in a string between "start" and "end" indices ,"end" index optional.
If "start" is greater than "end", this method will swap the two arguments, i.e. str.substring(1,4) == str.substring(4,1).
If either "start" or "stop" is less than 0, it is treated as if it were 0.

Syntax:

 string.substring(start,end)

 Parameter   Description
 start=>        Required.
The position where to start the extraction. First character is at index 0
 end=>          Optional. The position (up to, but not including) where to end the extraction. If omitted, it extracts the rest of the string

Example 1: 

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

 Out Put:

 ell

Example 2: 

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

 Out Put:

 llo world!

Example 3: 

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

 Out Put:

 Hello world!

Example 4: 

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

 Out Put:

 ell
Posted On: 21-Jun-2015 08:14
 Log In to Chat