First, let’s take a look at the introduction to the use of substring functions.
1. Substring
Substring requires at least one parameter, the first parameter is the starting position, and the second parameter is optional, and the end position.
There is only one parameter:
The code copy is as follows:
<meta charset="UTF-8" />
<script type='text/javascript'>
/**
* Substring function uses DEMO
*/
var str = 'Welcome to Wulin.com for guests';
var sub = str.substring(3);
alert(sub); // out: Children's shoes are visiting Wulin.com
</script>
Two parameters:
The code copy is as follows:
<meta charset="UTF-8" />
<script type='text/javascript'>
/**
* Substring function uses DEMO
*/
var str = 'Welcome to Wulin.com for guests';
var sub = str.substring(3,11);
alert(sub); // out: Children's shoes to Wulin.com
</script>
2. Substr
Substr also requires at least one parameter, the first parameter is the starting position, and the second parameter is optional, which is the length.
There is only one parameter:
The code copy is as follows:
<meta charset="UTF-8" />
<script type='text/javascript'>
/**
* Substring function uses DEMO
*/
var str = 'Welcome to Wulin.com for guests';
var sub = str.substr(3);
alert(sub); // out: Children's shoes are visiting Wulin.com
</script>
Two parameters:
The code copy is as follows:
<meta charset="UTF-8" />
<script type='text/javascript'>
/**
* Substring function uses DEMO
*/
var str = 'Welcome to Wulin.com for guests';
var sub = str.substr(3,2);
alert(sub); // out : kid
</script>
From this example above, it can be seen that when substring and substr have only one parameter, the results are the same, only if the second parameter is different.