The difference between JavaScript substr and substring
JavaScript substr and substring functions can be retrieved from a period in which the section of the string, but the use of both have different places, substr from the start node to take a particular string length, the range of parameters can be negative numbers, while the substring is It is taken from the beginning to the position before the set end character, and the range parameter cannot use negative numbers.
The basic syntax of JavaScript substr and substring
String.substr( Start, Length) String.substring( Start, End)
It can be seen from the basic syntax that the second parameter of substr is the length of the string, which can be set freely. If it is not filled in, it will be automatically taken to the last character of the String string. The second parameter of substring is the end character, which is automatically retrieved To the previous character of the character, if it is not filled in, the last character is also extracted.
Detailed usage explanation: examples of substr , substring
JavaScript substr and substring
var NewStr="Welcome to wibibi.";
//substr example
document.write(NewStr.substr(3)); // output come to wibibi.
document.write(NewStr.substr (3,6)); // output come t
//substring example
document.write(NewStr.substring(3)); // output come to wibibi.
document.write(NewStr.substring(3,6)); / / Output com
</script>
In the two methods, if only the Star parameter is set, it will capture to the end, and the output result will be the same, but if the second parameter is added, the result will be different! The substr in the example takes 7 characters in length, and the blank character is also considered as one, so the result of "come t" is output, and the substring is taken to the first character of the 6th character (the first character W starts from 0 Start counting) So output the result "com", which is the biggest difference between substr and substring .
Post a Comment
0 Comments