JavaScript substring
JavaScript substring can extract a string with a set range from a string, for example, start with the first character and end to the fifth character. The extraction range can be set by yourself, but the parameter cannot be a negative number , And cannot be two identical numbers, otherwise it will be an empty string, which is equivalent to no effect. The similar syntax is JavaScript substr .
JavaScript substring basic syntax
String.substring( Start, End)
String is a string to be intercepted. There are two parameters in the parentheses of substring, namely Start and End. These two parameters determine the range of the extracted string. The substring is automatically captured from Start, including Start The character of the end to the character before End, that is, the character that does not contain End. The position of the first character starts at 0. Neither the Start nor End parameters can be negative, otherwise it will be invalid. If the number of End is greater than Star, it does not matter, substring will automatically swap the two parameters.
The Start parameter is a required item, which is used to inform the substring from which position to start execution, and the End parameter is an optional item. If it is filled in, the substring will end to the End stop and will not include End. If it is not filled in, the substring is just Fetch directly from Start to the last character.
JavaScript substring example
var NewStr="Welcome to jasonseo."
document.write(NewStr.substring(3)); // output come to jasonseo
document.write(NewStr.substring(3,6) ); // output com
</script>
The example first prepares a new string NewStr, and then uses substring to start the string. The first test only uses the Start parameter, so the string starts from the third character to the end, and the second test adds more With the End parameter, you can see that the output result starts from the character "c" and is extracted to the fifth character (W counts from 0), that is, does not include the sixth character. This is the feature of JavaScript substring.
PS. The usage of JavaScript substring is similar to slice , but the difference is that JavaScript substring cannot accept negative range settings.
Post a Comment
0 Comments