Use of the js string truncation function (substring substr split)

Source: Internet
Author: User

Common string truncation functions in js include substring substr split slice. The following describes how to use these character truncation functions.

Use substring ()

Definition and usage
The substring method is used to extract characters of a string between two specified subscripts.
Syntax
StringObject. substring (start, stop)
Parameter description
Start is required. A non-negative integer that specifies the position of the first character of the substring to be extracted in the stringObject.
Optional. A non-negative integer is 1 more than the position of the last character of the substring to be extracted in the stringObject. If this parameter is omitted, the returned substring ends at the end of the string.
Return Value
A new string. The value of this string contains a substring of stringObject. Its content is all characters from start to stop-1, and its length is stop minus start.


1. substr (start, length); extract the length from start.

 

The Code is as follows: Copy code
Var str = 'it blog is a blog focused on IT programming! ';
Alert (str. substr (0, 5); // IT blog is

2. substring (start, end );

Extract from start to end (excluding end ).

The Code is as follows: Copy code


Var str = 'it blog is a blog focused on IT programming! ';
Alert (str. substring (0, 5); // IT blog is

Example

The Code is as follows: Copy code

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 (-01234"
Alert (str. substring (-1,-5 ));--------""

Use split or slice ()

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: substring ()
Function: String truncation. For example, substring () is used to obtain "Minidx" from "MinidxSearchEngine)

 
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

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.