Differences between slice(), substring(), and substr()

There are three commonly used character interception functions in js: slice(), substring(), and substr(). Let me introduce to you some usages of slice(), substring(), and substr() functions in character interception. Make a difference. Three functions for taking strings: slice(start,[end]), substring(start,[end]) and substr(start,[length]) Related attributes: slice() The first parameter represents the start position, the second Each 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 Positive value; if the first argument is equal to greater than the second argument, an empty string is returned...

1
<code class = "hljs" > </code>

 

There are three commonly used character interception functions in js: slice(), substring(), and substr(). Let me introduce to you some usages of slice(), substring(), and substr() functions in character interception. Make a difference.

Three functions for taking strings: slice(start,[end]), substring (start,[end]) and substr (start,[length]) related attributes:

The first parameter of slice() represents the start position, the second parameter represents the next position of the end position, and the length of the string extracted is the difference between the second parameter and the first parameter; if the parameter value is a negative number , then the value is added to the string length and converted to a positive value; if the first parameter is equal to or greater than the second parameter, an empty string is returned.

The first parameter of substring() 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 turned to 0; among the two parameters, take the smaller value as the start position, The length of the truncated string is the difference between the larger value and the smaller value.

substr() The first parameter represents the starting position, and the second parameter represents the length of the interception

PS: Strings are counted from 0

example:

<script type="text/ javascript "> var stmp = "rcinn.cn"; //Use a parameter alert(stmp.slice(3));//Start from the 4th character, intercept to the last character; return "nn.cn" alert(stmp.substring(3));//Start from the 4th character, intercept to the last character; return "nn.cn"

      //Use two parameters alert(stmp.slice(1,5))//Start from the 2nd character to the 5th character; return "cinn" alert(stmp.substring(1,5));// From character 2 to character 5; returns "cinn"

      //If only one parameter is used and it 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"

      //在上面的例子中我们可以看出slice()和substring()的用法是相同的       //返回的值也是一样的,但当参数为负数时,他们的返回值却不一样,看下面的例子       alert(stmp.slice(2,-5));//返回"i"       alert(stmp.substring(2,-5));//返回"rc"       //从上面两个例子可以看出slice(2,-5)实际上是slice(2,3)       //负5加上字符串长度8转换成正3(若第一位数字等于或大于第二位数字,则返回空字符串);       //而substring(2,-5)实际上是substring(2,0),负数转换为0,substring总是把较小的数作为起始位置。

      alert(stmp.substring(1,5))//从第2个字符开始,到第5个字符;返回"cinn"       alert(stmp.substr(1,5));//从第2个字符开始,截取5个字符;返回"cinn."

</script>

substr 和 substring方法的区别

<script type="text/javascript">  var str = "0123456789";//  alert(str.substring(0));//------------"0123456789"  alert(str.substring(5));//------------"56789"  alert(str.substring(10));//-----------""  alert(str.substring(12));//-----------""  alert(str.substring(-5));//-----------"0123456789"  alert(str.substring(-10));//----------"0123456789"  alert(str.substring(-12));//----------"0123456789"  alert(str.substring(0,5));//----------"01234"  alert(str.substring(0,10));//---------"0123456789"  alert(str.substring(0,12));//---------"0123456789"  alert(str.substring(2,0));//----------"01"  alert(str.substring(2,2));//----------""  alert(str.substring(2,5));//----------"234"  alert(str.substring(2,12));//---------"23456789"  alert(str.substring(2,-2));//---------"01"  alert(str.substring(-1,5));//---------"01234"  alert(str.substring(-1,-5));//--------""  alert(str.substr(0));//---------------"0123456789"  alert(str.substr(5));//---------------"56789"  alert(str.substr(10));//--------------""  alert(str.substr(12));//--------------""  alert(str.substr(-5));//--------------"0123456789"  alert(str.substr(-10));//-------------"0123456789"  alert(str.substr(-12));//-------------"0123456789"  alert(str.substr(0,5));//-------------"01234"  alert(str.substr(0,10));//------------"0123456789"  alert(str.substr(0,12));//------------"0123456789"  alert(str.substr(2,0));//-------------""  alert(str.substr(2,2));//-------------"23"  alert(str.substr(2,5));//-------------"23456"  alert(str.substr(2,12));//------------"23456789"  alert(str.substr(2,-2));//------------""  alert(str.substr(-1,5));//------------"01234"  alert(str.substr(-1,-5));//-----------""  </script>

函数:split()  功能:使用一个指定的分隔符把一个字符串分割存储到数组 例子:

str=”jpg|bmp|gif|ico|png”; arr=theString.split(”|”); //arr是一个包含字符值”jpg”、”bmp”、”gif”、”ico”和”png”的数组

函数:John()  功能:使用您选择的分隔符将一个数组合并为一个字符串 例子:

var delimitedString=myArray.join(delimiter); var myList=new Array(”jpg”,”bmp”,”gif”,”ico”,”png”); var portableList=myList.join(”|”); //结果是jpg|bmp|gif|ico|png

 函数:indexOf() 功能:返回字符串中匹配子串的第一个字符的下标

var myString=”JavaScript”; var w=myString.indexOf(”v”);w will be 2 var x=myString.indexOf(”S”);x will be 4 var y=myString.indexOf(”Script”);y will also be 4

var z=myString.indexOf(”key”);z will be -1

在网上看到另一种非常简单的方法,代码如下:

  function func(s, n) {     return s.replace(/([^x00-xff])/g, “$1a”).slice(0, n).replace(/([^x00-xff])a/g, “$1″);   }

这个方法非常巧妙,而且基本上是正确的。说“基本上”是因为它在取“123汉字测试”左边长度为 6 的子串时,它返回的是“123汉字”,而不是“123汉”。当然,这也并不一定就是问题,某些情况下需求可能就是这样。这个方法还可以再改进一下,如下:

 function func(s, n) {     return s.slice(0, n).replace(/([^x00-xff])/g, “$1a”).slice(0, n).replace(/([^x00-xff])a/g, “$1″);

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324772104&siteId=291194637