Home  >  Article  >  Web Front-end  >  Summary of JavaScript string methods

Summary of JavaScript string methods

韦小宝
韦小宝Original
2018-03-14 18:06:531429browse

This article talks about the JavaScript string method. If you don’t know the JavaScript string method or are interested in the JavaScript string method, let’s take a look at this article together. Okay Stop talking nonsense and get to the point!

Test array str="China";
The following methods do not affect each other's testing of str.

Intercept string

substr(start, length)
start: (required) to intercept The starting position of the substring, negative value: -1 refers to the last character in the string, -2 refers to the second to last character...
length: (optional) to intercept the length of the substring, omitting it means intercepting to the end
Return the new string.

console.log(str);                           //China

console.log(str.substr(1));                 //hina
console.log(str.substr(-1));                //a
console.log(str.substr(1,2));               //hi
console.log(str.substr(-3,1));              //i

slice(start, end[excluding the element at this position])
start: (required) to intercept the child The starting position of the string, negative value: -1 refers to the last character in the string, -2 refers to the second to last character...
end: (optional) The end position of the substring to be intercepted (excluding this position) , omitting means intercepting to the end
Return a new string.

console.log(str);                           //China

console.log(str.slice(1));                  //hina
console.log(str.slice(1,2));                //h
console.log(str.slice(1,-1));               //hin

console.log(str.slice(-1));                 //a
console.log(str.slice(-3,-1));              //in
console.log(str.slice(-3,3));               //i

substring(start, end)Integer interception
start: (required) Required Intercept the starting position of the substring, negative value: -1 refers to the last character in the string, -2 refers to the penultimate character...
end: (optional) The end position of the substring to be intercepted (excluding the position), omitting means intercepting to the end
Return a new string.
Note: The difference between substring and slice is that substring does not accept negative parameters (so-called not accepted: negative parameters can be passed, but the result is incorrect)

console.log(str)                            //China 

console.log(str.substring(1))               //hina    
console.log(str.substring(1,3))             //hi

Search characters String

charAt(index)
index: (required) Search string subscript
Returns the character at index position

//w3c给的标准是必选,但是浏览器是支持的,无参相当于传入0
console.log(str.charAt());                  //C,无参,默认0
console.log(str.charAt(1));                 //h
console.log(str.charAt(9));                 //'',不在范围,空

charCodeAt(index)
index: (Required) The Unicode encoding of the character at the specified position of the searched string index
. This return value is an integer between 0 and 65535

console.log(str.charCodeAt(1));             //104(h)
console.log(str.charCodeAt());              //67(C),无参,默认0
console.log(str.charCodeAt(9));             //Nan(''),不在范围,空

indexOf(searchValue, fromIndex)
searchValue: (required) Search substring
fromIndex: (optional) The starting position of the search, omitting it means position 0
Returns the position where the searchValue substring first appears in the string (str), otherwise returns -1

console.log(str.indexOf('c'));              //-1,区分大小写
console.log(str.indexOf('h'));              //1
console.log(str.indexOf('h', 2));           //-1

lastIndexOf(searchValue, fromIndex)
This method searches the string from the back, the indexOf() method searches the string from the front, and the other two are the same.

var s = 'China,china';
console.log(s.lastIndexOf('A'));            //-1
console.log(s.lastIndexOf('i'));            //8
console.log(str.lastIndexOf('asi'));        //-1
console.log(s.lastIndexOf('c', 2));         //-1

search(str/regexp)
str/regexp: (required) The substring to search or the regular expression to match Formula
Returns the position of the first occurrence of the substring, or the first matching position of the regular expression (global g is not supported). Otherwise, -1 is returned.

console.log(str.search(/ch/));              //-1
console.log(str.search(/Ch/));              //0
console.log(str.search(/ch/i));             //0,/i大小写忽略

console.log(str.search('a'));               //5
str.concat(str1,str2…)

str is the first string of the connection, str1 is the second string, str2 is the third string, and so on.
Return to new string

var aa = 'aa';
var bb = 'bb';
var cc = 'cc';

console.log(cc.concat(aa, bb)); //ccaabb    
console.log(cc);                //cc

The above is all the content of this article. If you don’t know much about it, you can implement both sides yourself and it will be easy to master!

Related recommendations:

Detailed introduction to JavaScript strings

Summary JavaScript Example tutorial of character string retrieval

The above is the detailed content of Summary of JavaScript string methods. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn