Comparison of the js string truncation functions slice, substring, and substr, slicesubstr

Source: Internet
Author: User

Comparison of the js string truncation functions slice, substring, and substr, slicesubstr

In js, the character truncation function has three common slice (), substring (), and substr (). Next I will introduce slice () and substring () and substr () functions are used and different in character truncation.

Three Functions of the string:Slice (start, [end]), substring (start, [end]) and substr (start, [length])
Related attributes:
Slice ()
The first parameter represents the start position, and the second parameter represents the next position of the end position. The length of the truncated string is the difference between the second parameter and the first parameter. If the parameter value is negative, the value is converted into a positive value after the string length is added. If the first parameter is equal to or greater than the second parameter, an empty string is returned.
Substring ()
The first parameter indicates the start position, and the second parameter indicates the next position of the end position. If the parameter value is negative, the value is converted to 0, take a smaller value as the starting position, and the length of the truncated string is the difference between a smaller value and a smaller value.
Substr ()
The first parameter indicates the start position, and the second parameter indicates the truncation length.
PS: the string starts from 0.
Example:

<Script type = "text/javascript"> var stmp = "rcinn.cn"; // use a parameter alert (stmp. slice (3); // starts from 4th characters and truncates to the last character; returns "nn.cn" alert (stmp. substring (3); // starts from 4th characters and truncates to the last character. Return "nn.cn" // use two parameters alert (stmp. slice (2nd) // starts from 5th characters to characters; "cinn" alert (stmp. substring (2nd); // starts from 5th characters to characters; returns "cinn" // if only one parameter is used and it is 0, then the whole parameter alert (stmp. slice (0); // returns the entire string alert (stmp. substring (0); // returns the entire string // returns the first character alert (stmp. slice ( 0, 1); // returns "r" alert (stmp. substring (); // return "r" // in the preceding example, we can see slice () and substring () the usage of the same // return value is the same, but when the parameter is negative, their return values are different. See the 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) // convert negative 5 with String Length 8 to positive 3 (if the first digit is equal to or greater than the second digit, an empty string is returned); // while substring (2,-5) it is actually substring (), and the negative number is converted to 0. substring always uses a small number as the starting position. Alert (stmp. substring (2nd) // starts from 5th characters to characters; returns "cinn" alert (stmp. substr (2nd); // starts from characters, truncates 5 characters; returns "cinn. "</script>

Differences between substr and substring Methods

<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>

Function:Split ()
Function: uses a specified separator to split a string and store it in 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: combines an array into a string using the separator you selected.
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 | png

Function:IndexOf ()
Function: returns the subscript of the first character matching the substring in a 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

You can see another very simple method on the Internet. 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 basically correct. Basically, this is because it returns "123 Chinese characters" instead of "123 Chinese characters" for a substring with a length of 6 on the left side of "123 Chinese characters test ". Of course, this is not necessarily a problem. In some cases, this may be the case. 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 the content of this article, and I hope it will help you learn javascript programming.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.