Some operations of string, such as arrays

Source: Internet
Author: User
Tags string indexof

1. String cutting

var a= "Hello World";//a.length=11
Alert (A.slice (3));
Alert (a.substring (3));
Alert (A.SUBSTR (3));//Three lo World

Alert (A.slice (3,7));//3 to 7
Alert (a.substring (3,7));//3~7
Alert (A.SUBSTR (3,7))//3 back 7 characters

Alert (A.slice ( -3));//a.length-3 begins, 11-3=8= "Rld";
Alert (a.substring ( -3));//substr will be regarded as 0,hello world;
Alert (A.SUBSTR (-3));//Same as slice mode

Alert (A.slice (3,-4));//(3,a.length (11-4=7)) Lo W
Alert (a.substring (3,-4)),//As (3,0), and its own adjustment to (0,3) characteristics, hel
Alert (A.SUBSTR (+3,-4));///also as (3,0), the following parameter is 0 nature is "" (empty string)

2 string IndexOf

var a= "Hello World";

A.indexof ("O");//4

A.lastindexof ("0");//7

A.indexof ("O", 6)//At the beginning of the 6th

A.lastindexof ("O", 6)

3.trim () Remove spaces

4.toLowerCase () case

toUpperCase ()

5. Find inside the string

1) match () is the same as RegExp

var a= "Hello World";
var b=/lo/;
var c=a.match (b);
Console.log (C[0],c.index,c.lastindex);

2) Search () returns only the first position, like C.index

var a= "Hello World";
var b=/lo/;
var c=a.search (b);
Console.log (C[0],c.index,c.lastindex);//Three undefined
Console.log (c);

Easy replacement of 6 strings

var a= "Cat,bat,fat,dat";
var b=a.replace (/at/g, "fuck");
Console.log (b);

The second parameter can also be encoded with a segment to update the matching results

var a= "Cat,bat,fat,dat";
var b=a.replace (/(. at)/g, "Fuck ($)");

It can also be a functional function (Match,pos,originaltext) is a pattern match, match the position of the string, the original character (the previous match)

var a= "<000<0.00<>0<.0";
var b=a.replace (/[<>.] /g,function (Match,pos,originaltext) {
Switch (match) {
Case "<":
Case ">":
return "1";
Case ".":
return originaltext;//<>. These
}
});
Console.log (b);

7 Serializing arrays

var a= "Sdds,sf,we,zc";
var b=a.split (",");
var c=a.split (/[/,]/);
Console.log (B,C);

8 Comparison of strings

var a= "Fuck";
var b= "E";
Console.log (A.localecompare (b));//cannot be directly reduced

9 fromCharCode () method

String.fromCharCode (104,101,108,108,111)); encoding converts to character hello

Some operations of string, such as arrays

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.