JavaScript引用数据类型之基本包装类型Boolean、Number、String

JavaScript引用数据类型之基本包装类型

1、Boolean

var falseObj = new Boolean(false)        //创建Boolean对象

var falseValue = false

console.log(typeof falseObj);          //object

console.log(typeof falseValue);         //boolean

console.log(falseObj instanceof Boolean);    //true  布尔表达式中所有的对象都会被转换为true

console.log(falseValue instanceof Boolean);   //false 布尔值不是Boolean对象

2、Number

var numberObj = new Number(10)    //object
var num = 10;            //number
console.log(num.toString(2));    //'1010'  返回二进制数值的字符串形式
console.log(num.toString(8));    //'12'
console.log(num.toString(10));    //'10'
console.log(num.toString(16));    //'a'
var num = 10.005;         //按照指定的小数位返回数值的字符串
console.log(num.toFixed(1));    //10.0
console.log(num.toFixed(2));    //10.01

var num = 100;           //指数表示法
console.log(num.toExponential(1));//1.0e+2
console.log(num.toExponential(2));//1.00e+2

var num = 99;           //可能返回固定大小格式,也可能返回指数格式
console.log(num.toPrecision(1));  //1e+2
console.log(num.toPrecision(2));  //99

3、String

(1) 字符串的对象包装类型

var stringObj = new String('hello world')   

console.log(stringObj.length);

(2)字符方法:charAt、charCodeAt

var stringValue = 'hello world'

console.log(stringValue.charAt(6));          //w     返回指定位置的字符

console.log(stringValue.charCodeAt(6));      //119    返回指定位置的字符编码

console.log(stringValue[0]);                 //ie7后支持

(3)字符串操作方法:concat、slice、substring、substr

var stringValue = 'hello world'

var stringValue2 = '!'

console.log(stringValue.concat(stringValue2,'你好,世界!')); //hello world!   对字符串的拼接

console.log(stringValue.slice(3,7));         //lo w       切片 [开始,结束位置)

console.log(stringValue.substring(3,7));     //lo w       切片 [开始,结束位置)

console.log(stringValue.substr(3,7));        //lo worl    切片 [开始,字符个数)



console.log(stringValue.slice(-3));           //rld      slice将-3 (+11)转换为slice(8)

console.log(stringValue.substr(-3));          //rld      substr-3 (+11)substr(8)

console.log(stringValue.substring(-3));       //hello world  substring将-3转换为0,返回全部字符串

(4)字符串位置方法:indexOf、lastIndexOf

var stringValue = 'hello world'

stringValue.indexOf('o')     //4

stringValue.lastIndexOf('o') //7

(5)trim( ) :删除前置和后置的空格并返回结果

var stringValue = '    hello,world   '

stringValue.trim()             //hello,world   

(6)字符串的大小写转换:toLocaleUpperCase、toLocaleLowerCase、toUpperCase、toLowerCase

var stringValue = 'hello world'

stringValue.toLocaleUpperCase()  //HELLO WORLD

stringValue.toUpperCase())       //HELLO WORLD

stringValue.toLocaleLowerCase(0) //hello world

stringValue.toLowerCase()    	//hello world

(7)localeCompare()方法

var stringValue = 'yellow';

console.log(stringValue.localeCompare('brick'));//1   字母表中brick在yellow之前,字符串参数在字符串前,  返回1

console.log(stringValue.localeCompare('yellow'));//0   字母表中yellow等于yellow, 字符串参数等于字符串,  返回0

console.log(stringValue.localeCompare('zoo'))//-1    字母表中zoo在yellow之后,  字符串参数在字符串后面, 返回-1

(8)fromCharCode()方法:返回多个字符编码转换成的字符串

console.log(String.fromCharCode(104,101,108,108,111));//hello 

猜你喜欢

转载自blog.csdn.net/m0_47147246/article/details/125761360