What is the difference between substr and substring?

Viewed 285768

What is the difference between

alert("abc".substr(0,2));

and

alert("abc".substring(0,2));

They both seem to output “ab”.

11 Answers

substring(): It has 2 parameters "start" and "end".

  • start parameter is required and specifies the position where to start the extraction.
  • end parameter is optional and specifies the position where the extraction should end.

If the end parameter is not specified, all the characters from the start position till the end of the string are extracted.

var str = "Substring Example";
var result = str.substring(0, 10);
alert(result);

Output : Substring

If the value of start parameter is greater than the value of the end parameter, this method will swap the two arguments. This means start will be used as end and end will be used as start.

var str = "Substring Example";
var result = str.substring(10, 0);
alert(result);

Output : Substring

substr(): It has 2 parameters "start" and "count".

  • start parameter is required and specifies the position where to start the extraction.

  • count parameter is optional and specifies the number of characters to extract.

var str = "Substr Example";
var result = str.substr(0, 10);
alert(result);


Output : Substr Exa

If the count parameter is not specified, all the characters from the start position till the end of the string are extracted. If count is 0 or negative, an empty string is returned.

var str = "Substr Example";
var result = str.substr(11);
alert(result);

Output : ple

substring(startIndex, endIndex(not included))

substr(startIndex, how many characters)

const string = 'JavaScript';

console.log('substring(1,2)', string.substring(1,2)); // a
console.log('substr(1,2)', string.substr(1,2)); // av
let str = "Hello World"

console.log(str.substring(1, 3))  // el -> Excludes the last index
console.log(str.substr(1, 3))  // ell -> Includes the last index

EDIT: This answer is with reference to R programming

Here are major differences between substr() and substring():

  1. substr() has arguments start & stop while substring as arguments first & last.

    substr(x, start, stop)

and

substring(text, first, last = 1000000L)

EXAMPLE

substr("abcdef", start = 2, stop=4)
[1] "bcd"

substring("abcdef", first = 2, last = 4)    
[1] "bcd"
  1. substring function has a large default value [1000000L] of 'last' argument so you may skip specifying that while substr function needs you to specify the value of stop argument.

EXAMPLE

substr("abcdef", start = 2)
Error in substr("abcdef", start = 2) : 
  argument "stop" is missing, with no default

substring("abcdef", first = 2)
[1] "bcdef"
  1. If you apply substr function to several starting or stopping points, the function uses only the first entry (i.e. the stopping point 1) while substring function will extract several possible strings.

EXAMPLE

> substr('abcdef', 1:3, 5)
[1] "abcde"
> substr('abcdef', 1:3, 5:6)
[1] "abcde"
> substr('abcdef', 1, 5:6)
[1] "abcde"
> substring('abcdef', 1:3, 5)
[1] "abcde" "bcde"  "cde"  
> substring('abcdef', 1, 5:6)
[1] "abcde"  "abcdef"
> substring('abcdef', 1:3, 5:6)
[1] "abcde" "bcdef" "cde"  

Someone mention use of negative index/zero. Both are accepted by substr() and substring().

EXAMPLE

> substr('abcdef', -2, 3)
[1] "abc"
> substring('abcdef', -2, 3)
[1] "abc"
> substring('abcdef', 0, 3)
[1] "abc"
> substr('abcdef', 0, 3)
[1] "abc"

Important Note for using substr() or substring() for string replacement:

The replacement needs to have the same number of characters as the replaced part of your data. If you want to replace a substring with a string with different length, you might have a look at the gsub() function.

P.S. I am using R version 4.0.4

Related