In js, there are three commonly used slice(), substr(), and substr() in character intercept function. Let me introduce to you some usage and differences between slice(), substr(), and substr() functions when intercepting characters.
Three functions for taking strings: slice(start,[end]), substring(start,[end]) and substr(start,[length])
Related attributes:
slice()
The first parameter represents the starting position, the second parameter represents the next position of the end position, and the length of the intercepted string is the difference between the second parameter and the first parameter; if the parameter value is negative, the value is added to the string length and converted to a positive value; if the first parameter is equal to greater than the second parameter, an empty string is returned.
Substring()
The first parameter represents the start position, and the second parameter represents the next position of the end position; if the parameter value is negative, the value is converted to 0; among the two parameters, the smaller value is taken as the start position, and the length of the intercepted string is the difference between the larger value and the smaller value.
substr()
The first parameter represents the start position, and the second parameter represents the intercepted length
PS: All strings start from 0
example:
<script type="text/javascript"> var stmp = "rcinn.cn"; //Use one parameter alert(stmp.slice(3)); //From the fourth character, intercept the last character; return "nn.cn" alert(stmp.substring(3)); //From the fourth character, intercept the last character; return "nn.cn" //Use two parameters alert(stmp.slice(1,5))//From the second character, to the fifth character; return "cinn" alert(stmp.substring(1,5)); //From the second character, to the fifth character; return "cinn" //If only one parameter is used and is 0, then return the entire parameter alert(stmp.slice(0));//Return the entire string alert(stmp.substring(0));//Return the entire string//Return the first character alert(stmp.slice(0,1));//Return "r" alert(stmp.substring(0,1));//Return "r" //In the above example, we can see that the usage of slice() and substring() are the same //The returned values are the same, but when the parameters are negative, their return values are different. See the following example alert(stmp.slice(2,-5));//Return "i" alert(stmp.substring(2,-5));//Return "rc" // From the above two examples, we can see that slice(2,-5) is actually slice(2,3) // Negative 5 plus string length 8 converts to positive 3 (if the first digit is equal to or greater than the second digit, an empty string is returned); // Substring(2,-5) is actually substring(2,0), and negative digits are converted to 0, substring always takes a smaller number as the starting position. alert(stmp.substring(1,5))//Start from the second character to the fifth character; return "cinn" alert(stmp.substr(1,5));//Start from the second character, intercept 5 characters; return "cinn."</script>
The difference between substr and substring methods
<script type="text/javascript"> var str = "0123456789";// alert(str.substring(0));//----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- alert(str.substring(-10));//--------------"0123456789" alert(str.substring(-12));//--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- alert(str.substring(2,2));//--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- alert(str.substr(5));//------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ alert(str.substr(0,10));//--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- alert(str.substr(-1,5));//--------------"01234" alert(str.substr(-1,-5));//-------------------"" </script>
Function: split()
Function: Use a specified separator to split a string into an array
example:
str=”jpg|bmp|gif|ico|png";arr=theString.split("|");//arr is an array containing character values "jpg", "bmp", "gif", "ico" and "png"Function: John()
Function: Use the delimiter of your choice to combine an array into a string
example:
var delimitedString=myArray.join(delimiter);var myList=new Array("jpg","bmp","gif","ico","png");var portableList=myList.join("|");//The result is jpg|bmp|gif|ico|pngFunction: indexOf()
Function: Return the subscript of the first character matching the substring in the string
var myString=”JavaScript”;var w=myString.indexOf(”v”);w will be 2var x=myString.indexOf(”S”);x will be 4var y=myString.indexOf(”Script”);y will also be 4var z=myString.indexOf(”key”);z will be -1
I saw another very simple method online, the code is as follows:
function func(s, n) { return s.replace(/([^x00-xff])/g, "$1a").slice(0, n).replace(/([^x00-xff])a/g, "$1"); }This method is very clever and is basically correct. It is said "basically" because when it takes a substring with length 6 to the left of "123 Chinese Character Test", it returns "123 Chinese Characters", not "123 Chinese". Of course, this is not necessarily a problem, and in some cases the requirements may be like this. This method can be improved as follows:
function func(s, n) { return s.slice(0, n).replace(/([^x00-xff])/g, "$1a").slice(0, n).replace(/([^x00-xff])a/g, "$1"); }The above is all about this article, I hope it will be helpful for everyone to learn JavaScript programming.