Js string truncation functions: slice (), substring (), and substr ()

Source: Internet
Author: User
Tags truncated

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:

The Code is as follows: Copy code

<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. "nn.cn" is returned"
Alert (stmp. substring (3); // starts from 4th characters and truncates to the last character. "nn.cn" is returned"

// Use two parameters
Alert (stmp. slice (2nd) // starts from 5th characters and ends with characters. "cinn" is returned"
Alert (stmp. substring (2nd); // starts from 5th characters and ends with characters. "cinn" is returned"

// If only one parameter is used and the value is 0, the whole parameter is returned.
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); // return "r"
Alert (stmp. substring (0, 1); // return "r"

// In the preceding example, we can see that slice () and substring () are used in the same way.
// The returned values are the same, but when the parameter is negative, their return values are different. See the example below.
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) is actually substring (2, 0), the negative number is converted to 0, and the substring always uses a smaller number as the starting position.

Alert (stmp. substring (2nd) // starts from 5th characters and ends with characters. "cinn" is returned"
Alert (stmp. substr (2nd); // starting from characters, truncates 5 characters; returns "cinn ."

</Script>


Differences between substr and substring Methods


Example:

The Code is as follows: Copy code
<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:

The Code is as follows: Copy code
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:

The Code is as follows: Copy code
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.

The Code is as follows: Copy code

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


You can see another very simple method on the Internet. The Code is as follows:

The Code is as follows: Copy code
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:

The Code is as follows: Copy code
Function func (s, n ){
Return s. slice (0, n ). replace (/([^ x00-xff])/g, "$ 1a "). slice (0, n ). replace (/([^ x00-xff]) a/g, "$1 ″);
}

However, even if this improved version is still less efficient than method 3 I have proposed above, because although it is easy to write, it sometimes makes unnecessary calculations.

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.