1. String
【 definition 】 Enclose single quotation marks or double quotes
Strings, like arrays, have numeric indexes and can be retrieved by the index of the response
- Gets the length of the string: str.length;
- Get the first character: STR [0]
- Get the last character: STR [str.length-1];
2, string common methods
Because strings are value data types and operate by value, they do not modify the original data
1), charAt:
- Function: Get the corresponding character of the corresponding position
- Parameter: index
- Return value: Returns the corresponding character
var str="zhufeng";
var res=str.charAt(0); ==>"z"
Copy the code
The difference between STR [index] and charAt if you write an index value that exceeds the length of the string
var str="zhufeng"; srt[100]===>undefined var res=str.charAt(100); = = = > ""Copy the code
2), charCodeAt:
- Function to obtain the corresponding ascII code value
- Parameter: index
- Return value: the corresponding ASCLL code decimal value
var str="xiaohui"; var res=str.charCodeAt(0); ASCII code address: https://baike.sogou.com/v53369.htm?fromTitle=ASCIICopy the code
3) indexOf
Gets the index of the first occurrence of a particular character in a string
Parameter: specific character
Return value: specifies the first occurrence of the index position if it is included, or -1 if it is not;
4) the lastIndexOf
Same as above, except this is the index of the last occurrence of a particular character in the string
var str="chengxiaohui"; var res1=str.indexOf("c"); ===>0 var res2=str.indexOf("i"); ===>-1 var res=str.lastIndexOf(""); = = = > 9Copy the code
Using the above two methods, you can determine whether a particular character is contained in a string
var str="zfpx"; If (str.indexof ("z")>-1){// If (str.indexof ("z")>-1){Copy the code
5) slice
Finds a character at a specific position in a string
Parameters: (n,m) n: start index (inclusive); M End index (not included)
Return value: the character searched
- Searches for characters from index n (inclusive) to the end of index m (inclusive)
- So if you don’t write m, you’re looking up to the end
- If you just write a zero in n, or you don’t write it, you copy it
- Str.length + negative index value
Var STR ="chengxiaohuiaichangfu" str.slice(1,3) ===>"he" str.slice(0) ===> copy a str.slice() ===> copy a var res=str.slice(-3,-1); = = = > "gf"Copy the code
6)substring(n,m)
SubString and Slice are basically the same, except that subString does not support negative indexes, while Slice does
7) substr (n, m)
- Function: Intercepts m characters from index n
- Parameters: n, m
- Return value: truncated string
- Starting with negative indexes is also supported
var str="chenghui"; Var res = STR. Substr (3, 2); ====>"hu"Copy the code
8) toUpperCase()
ToUpperCase (): converts the string toUpperCase
9) toLowerCase()
ToLowerCase (); Converts a string to lowercase
10) replace()
- Function: To replace one part of a string with another
- Arguments :(str1, str2) the first argument represents the character to be replaced or the re; The second argument represents the replaced character
- Return value: the replaced string
var str="xiaohui2018xiaohui2019xiaohui";
var res=str.replace("xiaohui","changfu");
//===>"changfu2018xiaohui2019xiaohui"
var res=str.replace(/xiaohui/g,"changfu");
console.log(res)===>"changfu2018changfu2019changfu"
Copy the code
11) split()
- Splits a string into arrays of specified characters
- Parameter: separator
- Return value: Split array
Split and join compare memories
var str="1-2-3"; var res=str.split("-"); console.log(res); = = = > [" 1 ", "2", "3"]Copy the code
3. Exercises
1, [time string processing] change the following string to “August 18, 2019 12:32:18”
var str="2019-8-18 12:32:18"; //var res=str.split(/-| |:/g) var time=str.split(" "); console.log(time) //["2019-8-18", "12:32:18"] var timeLeft=time[0]; var timeRight=time[1]; var ary1=timeLeft.split("-"); // ["2019", "8", "18"] var ary2=timeRight.split(":"); / / / "12", "32", "18"] var result = ary1 [0] + "years" + ary1 [1] + "month" + ary1 [2] + ", "+" "+ ary2 [0] +" "+ ary2 [1] +" points "+ ary2 [2] +" seconds "is the console. The log (result) // function zero(num){return num<10? 0"+num:num; } var ss=zero(11); Var result = zero (ary1 [0]) + "year", a zero (ary1 [1]) + "month", a zero (ary1 [2]) + ", "+" "+ zero (ary2 [0]) +" when "+ zero (ary2 [1]) +" points "+ zero (ary2 [2]) +" seconds "/ /" August 18, 2019, 12 32 points, 18 seconds"Copy the code
2, queryURLParams question mark parameter processing
/* var str ="https://www.baidu.com?name=zhufeng&age=10&id=14"; { name:"zhufeng", age:10, id:14 } */ function urlParams(str){ var obj={}; var paramsStr=str.split("?" ) [1]; if(paramsStr){ //[name=zhufeng,age=10,id=14] var paramsAry=paramsStr.split("&"); for(var i=0; i<paramsAry.length; i++){ //name=zhufeng var item=paramsAry[i]; //[name,zhufeng] var itemAry=item.split("="); obj[itemAry[0]]=itemAry[1]; } } return obj; } var str ="https://www.baidu.com?name=zhufeng&age=10&id=14"; var result= urlParams(str);Copy the code
4. Common Math methods
Math is called a mathematical function, which is also object type data, primarily used to manipulate numbers
1) Math.abs(
Math.abs(-1)
2) math.ceil/math.floor rounded up, rounded down
Round up, either positive or negative, take the largest value
I’m going to round down, whether it’s positive or negative, and I’m going to take the smallest value
Math. Ceil (1.2) / / 2 math.h ceil (1.6)/Math / - 1. The floor (1.8) / / 1 math.h floor / / - 2 (1.1)Copy the code
3)Math.round()
- If it’s positive, it’s normal, you know, but if it’s negative, the critical point has to be greater than 5
Math. Round (1.5) / / 2 math.h round (1.5) / / - 1 Math. Round / / - 2 (1.51)Copy the code
4) Math.sqrt() square
Math.sqrt(9)
Copy the code
5) math.h pow exponentiation (n, m)
N to the m
Math.h pow (3, 2) = = > 9Copy the code
6)Math.PI
Math. PI = = = > 3.141592653589793Copy the code
7) math.max/math.min get maximum and minimum values
Math. Max (1, 2, 3) Math. Min (4 and 6)Copy the code
Math.random() returns a random number between 0 and 1 (greater than or equal to 0, less than 1)
Get random number between n and m: math.random ()*(m-n)+n;
Math.random()*10+10 a.math. random = [0,1]; b. Select [min, Max] as the following formula: Math.floor(math.random ().(max-min+1)+min) c. Select a random integer from [min.max) using the following formula: Math.floor(math.random ().(max-min)+min) d. Math.floor(math.random ().(max-min)+min+1)Copy the code
[Upgrade 2]
If the argument passed contains a string, it becomes a number. If it is a non-valid number, it is skipped
function fn(){ var total=null; for(var i=0; i<arguments.length; i++){ var item=Number(arguments[i]); isNaN(item)? null:total+=item } return total; }Copy the code
[Advanced Version 3: ES6]
function fn(... arg){ return eval(arg.filter((item)=>! IsNaN (item)). The join (" + "))} var res = fn (1, 2, 3, "3", "3 p");Copy the code