JS string intercept function (substring substr split) usage instructions

Source: Internet
Author: User

Using substring ()

Definitions and usage
The substring method is used to extract characters from a string mediation between two specified subscripts.
Grammar
Stringobject.substring (Start,stop)
Parameter description
Start Required. A non-negative integer that stipulates the position of the first character of the substring to be extracted in the stringobject.
Stop is optional. A non-negative integer that is 1 more than the last character of the substring to extract in the Stringobject. If this argument is omitted, the returned substring continues to the end of the string.
return value
A new string that contains a substring of stringobject whose contents are all characters from the start to the stop-1, whose length is stop minus start.


1.substr (start, length), length is intercepted 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);

Intercepts from start to end (does not contain an 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

Cases

The code is as follows Copy Code

var str = " 0123456789 ";
Alert (str.substring (0)),------------"0123456789"
Alert (str.substring (5)),------------"56789"
Alert ( str.substring)-----------""
Alert (str.substring),-----------""
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)),----------"a"
alert ( Str.substring (2,2)),----------""
Alert (str.substring (2,5)),----------"234"
Alert (str.substring); -------"23456789"
Alert (2,-2),---------"str.substring"
Alert (str.substring ( -1,5)),---------"01234"
Alert (str.substring ( -1,-5));--------"

Use split or slice ()

Function: Split ()
Function: To store a string partition into an array using a specified delimiter
Example:

The code is as follows Copy Code
Str= "Jpg|bmp|gif|ico|png";
Arr=thestring.split ("|");
Arr is an array that contains the character values "JPG", "BMP", "GIF", "ico", and "PNG"

Function: John ()
Function: Merges an array into a string using the separator of your choice
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 interception, for example, to get "Minidx" from "minidxsearchengine" to use SUBSTRING (0,6)


Function: IndexOf ()
Function: Returns the subscript of the first character in a string that matches a substring

  code is as follows copy code

var Mystring= "JavaScript";
var w=mystring.indexof ("V"); W'll be 2
var x=mystring.indexof ("S"); x'll be is 4
var y=mystring.indexof ("Scrip T "); y would also be 4

var z=mystring.indexof (" key "); Z'll be-1

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.