//引用类型和基本包装类型的主要区别就是对象的生存期
var s1 = "some text";
s1.color = "red";
var s2 = s1.substring(2);
console.log(s1.color); //undefined 相当于自动执行了s1=null;
var s3 = new String("some text");//基本包装类型
s3.color = "red";
console.log(s3.color);//red
//Boolean 类型
//Number 类型
//String 类型
var str = "hello world";
str.color = "red";
str = str.concat("!") //连接符,如果不赋给 str 则原来str的值不变
console.log(str.length); //11
console.log(str.charAt(2)); //l
console.log(str.charCodeAt(2));//108
console.log(str[3]);//l
console.log(str.color);//undefined
console.log(str.slice(2));//llo world!
console.log(str.substring(4));//o world!
console.log(str.slice(3,7));//lo w
console.log(str.substring(4,7));//o w
console.log(str.indexOf("o"));//4
console.log(str.lastIndexOf("o"));//7
console.log(str.indexOf("o",6));//7
console.log(str.lastIndexOf("o",5));//4
console.log(str.toLocaleUpperCase()); //"HELLO WORLD"
console.log(str.toUpperCase()); //"HELLO WORLD"
console.log(str.toLocaleLowerCase()); //"hello world"
console.log(str.toLowerCase()); //"hello world"
//字符串的模式匹配
var text = "cat,bat,sat,fat";
var pattern = /.at/
var matches = text.match(pattern);//返回第一个匹配项
console.log(matches); //["cat", index: 0, input: "cat,bat,sat,fat"]
var pos = text.search(/at/);//返回第一个匹配项的索引,若果有返回1,没有返回-1
console.log(pos);//-1
var repl =text.replace('at','ba');
console.log(repl); //cba,bat,sat,fat
var replg =text.replace(/at/g,'ba');
console.log(replg); //cba,bba,sba,fba
console.log(String.fromCharCode(104,101));//he //String 构造函数的静态方法
网友评论