js string object properties and methods overview

1. .length----Get the length of the string;

var myString="JavaScript";
console.log(myString.length); //10

2. concat----connect the string to generate a new string;

var s1="a";
var s2="b";
var s3="c";
console.log(s1.concat(s2,s3));  //abc
console.log(s1)  //a

3. indexOf (str, fromIndex) ---- find a match and return the index value, if not found, return -1;
common methods:

var myString="JavaScript";
console.log(myString.indexOf("v"));  //2
console.log(myString.indexOf("Script"));  //4
console.log(myString.indexOf("key"));  //-1

Complete indexof usage:

表示从索引位置fromIndex开始查找,如果fromIndex省略,则表示默认从起始索引0开始查找; 若fromIndex为负,则从索引0开始查找。
console.log(myString.indexOf("v",5));  //-1
console.log(myString.indexOf("v",1));  //2

4. charAt (index) ---- returns the character at the specified index position, if the index is out of bounds, returns an empty string;

myString="JavaScript";
console.log(myString.charAt(1));  //a
console.log(myString.charAt(10000000000000000));  //若索引越界,返回空字符串  -- ""
console.log(myString.charAt(-1));  //若索引越界,返回空字符串  -- ""

5. substr(fromIndex,length)---- intercepts a string of length length from the starting index fromIndex, the obtained string contains the value of the starting position of the index value, if the length length is not specified or exceeds the maximum length that can be intercepted , then intercept to the end, if the starting index fromIndex is negative, intercept from right to left, -1 means the last one;

myString="JavaScript";
console.log(myString.substr(1,1));  //a  
console.log(myString.substr(1,2));  //av
console.log(myString.substr(1));  //avaScript
console.log(myString.substr(1,4000000));  //avaScript
console.log(myString.substr(-1,1));  //t 
console.log(myString.substr(-2,1));  //p 
console.log(myString.substr(-6,2));  //Sc 
console.log(myString.substr(-6,6));  //Script

6. substring(startIndex,endIndex)----Intercept the substring from the start index startIndex to the end index endIndex, the result contains the characters at startIndex, but does not include the characters at endIndex, if endIndex is omitted, it is intercepted to the end, if If startIndex or endIndex is negative, it will be replaced with 0. If startIndex = endIndex, an empty string will be returned. If startIndex > endIndex, the two values ​​will be exchanged when the method is executed;

myString="JavaScript";
console.log(myString.substring(1,3));  //av
console.log(myString.substring(4));  //Script
console.log(myString.substring(-1,1));  //J
console.log(myString.substring(3,3));  //返回空
console.log(myString.substring(3,1));  //等价于myString.substring(1,3)

7. slice(startIndex,endIndex)----Intercept the substring from the start index startIndex to the end index endIndex, the result contains the characters at startIndex, excluding the characters at endIndex, the basic usage is the same as that of substring;

不同点:
myString="JavaScript";
console.log(myString.slice(1,3))   //av
如果startIndex > endIndex,则执行方法时返回空字符串
如果 start 为负,将它作为 length + start处理,此处 length 为数组的长度;
    console.log(myString.slice(-1,3))    //返回空
如果 end 为负,就将它作为 length + end 处理,此处 length 为数组的长度;
    console.log(myString.slice(2,-3))   //vaScr

8. split()---- split the string into an array and return a new array without affecting the original string;

var s="a,bc,d";
console.log(s.split(","));  //["a", "bc", "d"]
s="a1b1c1d1";
console.log(s.split("1"));  //["a", "b", "c", "d", ""]

9. join()----Use the selected delimiter to combine an array into a string;

var myList=new Array("jpg","bmp","gif","ico","png");
var imgString=myList.join("|");   
console.log(imgString)   //jpg|bmp|gif|ico|png

9. toLowerCase/toUpperCase----string case conversion;

myString="JavaScript";
console.log(myString.toLowerCase()) //javascript
console.log(myString.toUpperCase()) //JAVASCRIPT

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324779747&siteId=291194637