We have introduced the data types of javascript before. Today we will review the old ways through some examples, hoping that everyone can reach the point of knowing new things.
The code copy is as follows:
<script type="text/javascript">
//1. Boolean type
//2. Number type
//3, String type
//Boolean types are easily confused with basic types, so it is recommended not to use Boolean objects.
//Number is the reference type corresponding to the number
var numberObj = new Number(10);
//Rewrite the toString method and pass the parameter to tell it to put back the string type of the number in several digits
var num = 10;
alert(num.toString());//"10"
alert(num.toString(2));//"1010"
alert(num.toString(8));//"12"
alert(num.toString(10));//"10"
alert(num.toString(16));//"a"
//toFixed() method is a string representation method that returns the value of the specified decimal places, and has the function of rounding
var num = 10;
num.toFixed(2);//"10.00"
//toExponential() exponential representation method accepts a parameter to represent the number of decimals in the output result
var num = 10;
alert(num.toExponential(1));//"1.0e+1"
//However, for such a small number, you don't need to use exponential representation. If you want to get the most suitable format for a certain value, you should use it
//toPrecision() method, this method may return a fixed size format or an exponential format
//Accept a parameter to represent the number of digits of all numbers of the numeric value (excluding the exponential part).
var num = 99;
alert(num.toPrecision(1));//1e+2, 1e+2 means 100, because the index cannot represent 99, rounding upwards becomes 100
alert(num.toPrecision(2));//"99"
alert(num.toPrecision(3));//"99.0"
//String object, String object methods can also be accessed in all basic strings.
//1. Character operation methods: charAt(), charCodeAt(). Each parameter accepts a character position based on position 0
var stringValue = "Hello world!";
stringValue.charAt(1);//"e" The second position is "e"
stringValue.charCodeAt(1);//"101" The character encoding of the second position "e" is "101"
//2. String operation methods concat (split characters), slice (index, index), substring (index, index), substr(index, length). index:position, length: length: length
var str1 = "hello";
alert(str1.concat(" word"));//Hello world
alert(str1.concat(" word", "!"));//Hello world!
var stringValue = "Hello world!";
alert(stringValue.slice(3));//lo world
alert(stringValue.substring(3));//lo world
alert(stringValue.substr(3));//lo world
alert(stringValue.slice(3, 7));//lo w
alert(stringValue.substring(3, 7));//lo w
alert(stringValue.substr(3, 7));//lo world This 7 represents the intercepted length
//3. String position methods indexOf() and lastIndexOf()
// Both methods search for the given string from the specified string, and then return the position of the string, and return -1 if it is not found.
//The difference between these two methods is that one is to search the string backward from the beginning of the string, while lastIndexOf is to search the string forward from the end of the string.
//These two methods have an optional parameter (start the search from the specified location)
var stringValue = "hello word";
alert(stringValue.indexOf("o"));//4
alert(stringValue.lastIndexOf("o"));//7
//Can loop call indexOf or lastIndexOf to find the specified string
var stringValue = "wo de wei lai bu shi meng!wo men you geng hao de ming tian!";
var positions = [];
var pos = stringValue.indexOf("e");
while (pos > -1) {
positions.push(pos);
pos = stringValue.indexOf("e", pos + 1);
}
alert(positions);//4, 7, 22, 33, 38, 47
//4. The trim() method will create a copy of the string and delete all the spaces in the front and rear positions.
var stringValue=" hello word ";
alert(stringValue);
alert(stringValue.trim());
//5. String case conversion method
//toLowerCase, toLocalLowerCase, toUpperCase, toLocalUpperCase
var stringValue="hello word";
alert(stringValue.toLocaleUpperCase());//This method is relatively safe
alert(stringValue.toUpperCase());
alert(stringValue.toLocaleLowerCase());//This method is relatively safe
alert(stringValue.toLowerCase());
//6. String matching method replace()
//This method accepts two parameters. The first parameter is a regular expression or string, and the second parameter is a string or a function
var text="cat,bat,sat,fat";
var result=text.replace("at","ond");//
alert(result);//"cond,bond,sond,fond"
var result=text.replace(/at/g,"ond");//
alert(result);//"cond,bond,sond,fond"
var text="cat,bat,sat,fat";
result=text.replace(/(.at)/g,"word ($1)");
alert(result);
//The second parameter of replace can also be a function
function htmlEscape(text) {
//The function has three parameters: 1. Pattern matching item 2. The position of the pattern matching item in the character 3. The original string
return text.replace(/[<>"&]/g,function(match,index,text){
switch (match){
case "<":
return "<";
case ">":
return ">";
case "&":
return "&";
case "/"":
return "";
}
});
}
alert(htmlEscape("<p class=/"greeting/">Hello World!</p>"));
//<p class=greeting>Hello World!</p>
//localCompare() compares two strings. A.localCompare("B")
// If the string (A) is ranked before the string parameter (B) in the alphabet, this returns a negative number (-1)
//If the string is equal to the string parameter, return 0
//If the string (A) is arranged after the string parameter (B) in the alphabet, then the positive number (1)
var stringValue="f";
alert(stringValue.localeCompare("d"));//1
alert(stringValue.localeCompare("f"));//0
alert(stringValue.localeCompare("z"));//-1
//fromCharCode This static method performs the opposite operation to charCodeAt
alert(String.fromCharCode(104,101,108,108,111));//"hello"
//7. It is recommended not to use the html method.
</script>
END
Have you gained a new understanding of JavaScript data types? I hope you can like it.