Javascript truncation functions (indexOf, join, etc.) _ javascript skills

Source: Internet
Author: User
Below are some of the functions that are frequently used to intercept strings in js. Every function provides an example here. For more information, see. Function: indexOf ()
Function: returns the subscript of the first character matching the substring in a string.

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.

The Code is as follows:


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

The Code is as follows:


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.

The Code is as follows:


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.