JS truncation functions (indexof, join, etc)

Source: Internet
Author: User
Tags truncated

Function: indexof ()
Function: returns the subscript of the first character matching the substring in a string.

CopyCode The Code is as follows: 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: Split ()
Function: uses a specified separator to split a string and store it in an array.Copy codeCode: Str = "JPG | BMP | GIF | ICO | PNG ";
Arr = thestring. Split ("| ");
// Arr is an array containing character values "jpg", "BMP", "GIF", "ICO", and "PNG"

Function: Join ()
Function: combines an array into a string var delimitedstring = myarray. Join (delimiter) using the separator you selected );Copy codeCode: 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.

Functions: slice () and substring ()
Slice and substring can both accept one or two parameters. The 1st parameters are used to obtain the starting position of the string to be truncated, if the first parameter is not null, it is the first digit in the end position of the string to be truncated (that is, the obtained end position is not in the return value ), if it is null, It is truncated to the last character of the entire string. Copy code The Code is as follows: <script language = "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
// How can we reverse the first character? Other functions can be used. If you must use these two methods, set the first parameter to 0 and the second parameter to 1, see the following example.
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 () have the same usage and return the same value, but when the parameter is negative, their return values are different. Let's look at 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), and the negative 5 is converted into 3; and substring (2,-5) it is actually substring (), and the negative number is converted to 0. swubstring always uses the minimum number as the starting position.
</SCRIPT>

Note: The number of digits of a string starts from 0.

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.