Common methods and illustrative of the application of the string

Disclaimer: This article is a blogger original article, follow the CC 4.0 BY-SA copyright agreement, reproduced, please attach the original source link and this statement.
This link: https://blog.csdn.net/javascript_meng/article/details/100141084

Common methods:

1) charAt () Returns the specified character index position;

Example:

 var str='hello';
 console.log(str.charAt(4));

2) concat () connecting two or more of the String to return (stitching) after the connection;

Example:

var str='hello';
var s1=str.concat(s,s,s,s);
console.log(s1);

3) indexOf () returns the position in the string to retrieve the first occurrence of the specified character, not found return -1;

Example:

var str='hello';
var index=str.indexOf('l');
console.log(index);

4) lastIndexOf () returns the position of the specified character string to retrieve the last occurrence;

Example:

var str='hello';
var index=str.lastIndexOf('l');
console.log(index);

5) replace () Replace substring matching the regular expression, only the first alternative;

Example:

var str='hello';
var str0=str.replace('h','*');
var str0=str.replace('l','*');
console.log(str0);

6) slice () interception string, followed after closing the front opening;

Example:

var str='hello world';
var s=str.slice(0,2);
console.log(s);

7) split () string is divided into an array of sub-strings;

Example:

URL = var "http://baidu.com/index.jsp?username=lisi&pwd=123";
// find behind the content {username: lisi, pwd: 123 }?

var str='hello world hi';
 var str="hello,world,lisi";
 var str="username=lisi&pwd=123";
 var arr=str.split('&');
 var arr1=arr[0].split('=');
 console.log(arr,arr1);

8) substr () specifies the number of character strings extracted from a start index number;

Example:

var str='helYYYYlo';
var str1=str.substr(0,3);
console.log(s1);

9) substring () extracts a character string between the two index number specified;

Example:

var str='helYYYYlo';
var s1=str.substring(0,4);
console.log(s1);

10) toLocaleLowerCase () The host's locale lowercase string;

Example:

 var str='helYYYYlo';
 var s1=str.toLowerCase();
 console.log(s1);

11) toLocaleUpperCase () The host's locale convert a string to upper case;

Example:

 var str='helYYYYlo';
 var s1=str.toUpperCase();
 console.log(s1);

Guess you like

Origin blog.csdn.net/javascript_meng/article/details/100141084