Character string extraction method

1.slice

There are two parameters, the first parameter represents a start position, the second parameter represents the end position of the next, the length of the string is taken out of the difference between the second parameter and the first parameter;

If only the parameter value is negative, the value is a positive value plus the length of the string into

2.substring

There are two parameters, the first parameter represents a start position, the second parameter represents the end of the next position, if the parameter value is negative, the value is converted to 0;

To the smaller value of two parameters in the starting position

3.substr

The first parameter represents the start position, the second parameter represents a length, taken

Then we can raise your example

 

<script type="text/javascript">
      var stmp = "rcinn.cn";
      // use a parameter
      alert (stmp.slice (3)); // 4 characters from the beginning, the last characters of the intercepted; Back "nn.cn"
      alert (stmp.substring (3)); // from the first four characters, the last characters of the intercepted; Back "nn.cn"

      // use two parameters
      alert (stmp.slice (1,5)) // starting from the second character, the first five characters; Back "cinn"
      alert (stmp.substring (1,5)); // starting from the second character, the first five characters; Back "cinn"

      // If only one parameter and is 0, then return the entire parameter
      alert (stmp.slice (0)); // return the whole string
      alert (stmp.substring (0)); // return the whole string

      // returns the first character

      alert (stmp.slice (0,1)); // Returns "r"
      alert (stmp.substring (0,1)); // Returns "r"

      // In the example above we can see that the use of slice () and substring () is the same
      // return value is the same, but when the argument is negative, their return value is not the same, look at the following example
      alert (stmp.slice (2, -5)); // returns "i"
      alert (stmp.substring (2, -5)); // Returns "rc"
      // it can be seen that slice (2, -5) from the two examples above are actually slice (2,3)
      // string length plus minus 5 8 3 into positive (if the first number is equal to or greater than a second digit, the empty string);
      // the substring (2, -5) actually substring (2,0), a negative number is converted to 0, substring always smaller number as the start position.

      alert (stmp.substring (1,5)) // starting from the second character, the first five characters; Back "cinn"
      alert (stmp.substr (1,5)); // from the first two characters, 5 characters taken; return "cinn."

</script>

 

I finished sharing

 

 

 

 

 

Guess you like

Origin www.cnblogs.com/0428mm/p/12076386.html