Number type
(1) Create
Number is the reference type corresponding to the numeric value. To create a Number object, you can pass a numeric value into it when you call the Number constructor.
var numberObject = new Number(10);
Copy the code
(2) toString() method
You can use the toString() method to pass a radix argument that tells it to return a string of base values
var num = 10;
console.log(num.toString()); 10 "/ /"
console.log(num.toString(2)); / / "1010"
console.log(num.toString(8)); / / "12"
console.log(num.toString(10)); 10 "/ /"
console.log(num.toString(16)); //"a
Copy the code
(3) toFixed() method
var numOne = 10;
alert(numOne.toFixed(2)); / / "10.00"
var numTwo = 10.005;
alert(numTwo.toFixed(2)); / / "10.01"
Copy the code
The Number object usually provides important functionality for numeric values in the background. At the same time, we still don’t recommend instantiating the Number type directly.
Second, String type
(1) Create
The String type is an object wrapper for a String, which can be created using the String constructor as follows.
var stringObject = new String("hello world");
Copy the code
(2) Character methods charAt(), charCodeAt(), fromCharCode()
- CharAt () returns the character at the specified position.
- CharCodeAt () returns the Unicode encoding of the character at the specified position
- FromCharCode () converts Unicode encoding to characters
var str= 'hello world! ';//charAt() returns the character at the specified position.
str.charAt(1);//e
//charCodeAt() returns the Unicode encoding of the character at the specified position.
str.charCodeAt(1);/ / 101
//fromCharCode() converts Unicode encoding to characters.
String.fromCharCode(104.101.108.108.111); //"hello"
Copy the code
Concat (), slice(), substr(), substring()
- Concat () concates two or more strings and returns the new string
- Slice () extracts a fragment of a string and returns the extracted portion in a new string
- Substring () extracts the character between two specified index numbers in a string(similar to slice)
- Substr () extracts the number of characters in the string from the starting index number
var str= 'hello';
//1.concat() concatenates two or more strings and returns the new string
var result = str.concat(" world!");//"hello world!"
//2.slice() extracts a fragment of the string and returns the extracted portion in a new string
result.slice(3); //"lo world!"
result.slice(3.7); //"lo w" (7 represents index position)
//3.substring() extracts the character between two specified index numbers in a string(similar to slice).
result.substring(3);//"lo world!"
result.substring(3.7);//"lo w"
//4.substr() extracts a number of characters in the string from the starting index.
result.substr(3);//"lo world!"
result.substr(3.7);//"lo worl"
Copy the code
IndexOf(), lastIndexOf()
- IndexOf () returns the first occurrence of a specified string value in a string from front to back
- LastIndexOf () searches the string backwards and calculates the last occurrence of the returned string starting at position (0)
var str = "hello world!";
//indexOf() returns the position at which a specified string value first appears in the string
str.indexOf("o");/ / 4
str.indexOf("o".6);/ / 7
//lastIndexOf() searches the string from back to front, counting the last occurrence of the returned string starting at position (0).
str.lastIndexOf("o");/ / 7
str.lastIndexOf("o".6);/ / 4
Copy the code
(5) Trim () method
Removes whitespace from both sides of the string
var str =" hello ";
str.trim();//"hello"
Copy the code
(6) String case conversion methods toUpperCase(),toLowerCase()
- ToUpperCase () converts the string toUpperCase
- ToLowerCase () converts the string toLowerCase
var str = "hello world!";
str.toUpperCase();//"HELLO WORLD!"
str.toLowerCase();//"hello world!"
Copy the code
Search (), replace(), match(), split()
-
Search () finds the value that matches the regular expression
-
Split () splits a string into an array of strings
-
The match() lookup finds a match for one or more regular expressions
-
Replace () looks for matching substrings in the string and replaces the substrings that match the regular expression
var text = "cat, bat, sat, fat"
//search() looks for values that match the regular expression.
text.search(/at/);/ / 1
//split() splits a string into an array of strings.
result.split();//["hello world!"]
// Utility method, string inversion
result.split("").reverse().join(""); / / "! dlrow olleh"
//match() finds matches for one or more regular expressions.
text.match(/c/);//["c", index: 0, input: "cat, bat, sat, fat", groups: undefined]
//replace() looks for the matching substring in the string and replaces the matching substring in the regular expression.
text.replace("at"."ond");//"cond, bat, sat, fat"
text.replace(/at/g."ond");//"cond, bond, sond, fond"
Copy the code
(8) valueOf()
ValueOf () returns the original valueOf a string object.
var text = "cat, bat, sat, fat";
text.valueOf();//"cat, bat, sat, fat"
Copy the code