Difference between substr() and substring() in javascript - Java @ Desk

Thursday, August 8, 2013

Difference between substr() and substring() in javascript

var stringVar = "substr_vs_substring";
stringVar.substr(start, length);
stringVar.substring(start, stop);

substring(start, stop):It returns string from index position [start] i.e. 1 to index position [stop-1] i.e. 3
stringVar.substring(1 , 4) -> return "ubs";
substr(start, length):It returns string from index position [start] i.e. 1 to index position [length] i.e. 4. The length of the returned string matches with the length index of this method.
stringVar.substr(1 , 4) -> return "ubst";
As you can see the clear difference between the two is at the 2nd index i.e. length in case of substr and stop in case of substring.

substring returns the string from index position [start] to index position [stop-1]
substr returns the string from index position [start] to index position that equals [length] index.







No comments:

Post a Comment