The string processing function in JS split, join, SUBSTRING and indexof detailed

Source: Internet
Author: User

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

The code is as follows Copy Code

Example:

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.

Example

<script type= "Text/javascript" >
var arr = new Array (3)
Arr[0] = "George"
ARR[1] = "John"
ARR[2] = "Thomas"
document.write (Arr.join ("."))
</script>
Output:
George.John.Thomas

Function: substring ()

Function: string interception, for example, to get "Minidx" from "minidxsearchengine" to use SUBSTRING (0,6)


Example:

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

Alert (STR.SUBSTR (0));---------------"0123456789"
Alert (STR.SUBSTR (5));---------------"56789"
alert (STR.SUBSTR);--------------""
alert (STR.SUBSTR);--------------""
Alert (STR.SUBSTR ( -5));--------------"56789"
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));------------"9"
Alert (STR.SUBSTR ( -1,-5));-----------""

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'll 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.