基本包装类型

来源:互联网 发布:设计公司logo软件 编辑:程序博客网 时间:2024/06/06 05:55

1. 基本包装类型

Number, String, Bolean后台创建相应的对象,可以支持相应的内置对象方法

var box = "Mr Lee";

box.substring(2); //从第2个位置截取子字符串

box.name = "Lee";

box.age = function (){    //不能给基本包装类型加属性*

return 100;

}

var box = new String("Mr. Lee"); //上面替换成引用类型将有效

2. Number类型内置方法

toString()  //转换成字符串

toLocaleString() //根据本地格式化成字符串

toFixed(2)  //保留小数点后2位,转换成字符串(进行四舍无入)

toExponential() //指数形式转换成字符串

toPrecission(2)  //保留小数点后2位,可以使用指数或小数转化成字符串

3. String类型内置方法和属性

var box = "Mr. Lee";

box.length //内置属性代表字符串长度

box.charAt(2) //打印第2个位置的字符

box.charCodeAt(4)  //返回第2个位置的ansii码

box.concate("is", "a good teacher."); //连接字符串

box.slice(4, 6); //字符串的分片

box.subString(4,6) //同slice

box.substr(4, 6) //从第4个开始,选6个字符


box.slice(-2) //从倒数第二位开始选到最后一个字符串

box.subString(-2) //返回整个字符串

box.substr(-2) //同slice


box.indexOf("M") //返回找到的第一个索引值

box.lastIndexOf("M") //返回找到的最后一个索引值

box.indexOf("L", 5)  //从第5个位置开始向后搜索

box.lastIndexOf("L", 5) //从第5个位置向前搜索

找不到返回-1

4. 字符串的大小写转换

var box = "Mr. Lee";

box.toLowerCase();   //转换成小写

box.toUpperCase();  //转换成大写

5. 字符串匹配的方法

var box = "Mr. Lee";

box.match("Mr.")   //是否匹配, 没有找到为NULL

box.search("Mr.")  //找到Mr.的位置

box.replace("Mr.", "mr.") //替换Mr.为mr.

box.split(" ")  //使用空格分割成数组

6. String类型的静态方法

String.fromCharCode(76)  //ansii码转换成字符

var box = "Mr. Lee";

box.link("http://www.baidu.com")  //转换成html标签<a href="http://www.baidu.com">Mr. Lee</a>