typeof
typeof 'Hello World' ——> 'string'
typeof('Hello World') ——> 'string'
toString VS valueOf
var bar = {
valueOf: function() { return 10; },
toString: function() { return 'foo'; }
}
console.log(bar);
alert(bar);
alert(+bar);
toString VS String()
toString和String并不是一样的,但是也有一定的共通性,如
var o = { toString: function () { return "foo"; } };
String(o); // "foo"
看上去好像String构造函数只是简单的调用了参数的toString方法,但是其实不是这样的。又如
var value = null;
String(null); // "null"
value.toString(); // TypeError
String构造函数传递参数为一个对象,执行步骤为
1.如果有值,指定该对象的toString方法,如果执行过之后返回的值是基本数据类型,则返回该结果,如果不是,执行第二步。
2.执行该对象的valueOf方法,如果执行过之后返回的值是基本数据类型,则返回该结果,如果不是,抛出异常TypeError。
以上只是一个简单的介绍,更深入详细的内容参看Object-to-Primitive-Conversions-in-JavaScript、ToString
注:字符串对象原型链上面的方法大部分不是字符串独有的,可以当做方法转移到其他类型对象。
String.prototype.charAt
'Hello'.charAt(1);
String.prototype.charAt.call([1,2,3],1) //,
String.prototype.charCodeAt
'Hello'.charCodeAt(1).toString(16); //65,e的unicode码
String.prototype.charCodeAt.call([1,2,3],1).toString(16) //,的Unicode码
String.prototype.concat 基本同Array.prototype.concat
String.prototype.indexOf
'Hello World'.indexOf('o',5);注意第二个参数
String.prototype.lastIndexOf 同上
String.prototype.localeCompare
'我'.localeCompare('你'); //1
'a'.localeCompare('b'); //-1
'ab'.localeCompare('ab'); //0
String.prototype.match
'Hello2World'.match(/[eo]/g) //['e','o','o']
String.prototype.replace
基本用法
'Hello'.replace('ll','LL'); //HeLLo,原字符串不变
使用正则进行筛选
'a,b,c,d'.replace(/\w(?=,)/g,'O'); //"O,O,O,d"
替换文本使用正则匹配结果
var name = "Doe John";
name.replace(/(\w+)\s*(\w+)/g,'$2-$1($&)'); //"John-Doe(Doe John)"
"$1,$2".replace(/(\$(\d))/g, "$$1-$1$2") //"$1-$11,$1-$22"
字符 | 替换文本 |
---|---|
$n | 第n个匹配结果 |
$& | 与 regexp 相匹配的子串。 |
$` | 位于匹配子串左侧的文本。 |
$' | 位于匹配子串右侧的文本。 |
$$ | $符号 |
替换文本使用function
'a,b,c,d'.replace(/\w(?=,)/g,function(m){ //"A,B,C,d"
return m.toUpperCase();
});
var name = "Doe John";
name.replace(/(\w+)\s*(\w+)/g,function(m,$1,$2,index,str){
return $2-$1(m);
}); //"John-Doe(Doe John)
参数列表, 匹配内容+捕获的部分?+匹配的开始索引+完整的字符串
String.prototype.search 参数传递的是regExp,返回第一个匹配的索引,类似于indexOf
String.prototype.slice
var str = 'javascript';
str.slice(1,5); //截取索引1(包括)-5(不包括)之间的片段'avas'
str.slice(4); //截取下标4及之后的所有字符串'script'
str.slice(-3); //截取倒数最后第三个索引之后的所有片段'ipt'
str.slice(-5,-1); //截取倒数第五个索引到倒数第一索引(不含)之间的片段"crip",同str.slice(-5,9);
str.slice(5,1); //截取范围不对,返回空字符串
String.prototype.substring
var str = 'javascript';
str.substring(1,5);//同上
str.substring(4); //同上
str.substring(-3); //不支持传入负值,所以返回了整个字符串
str.substring(5,1); //截取范围索引不对的话会自动调转
String.prototype.substr
var str = 'javascript';
str.substr(1,5); //截取下标为1之后长度为5的字串,'avasc'
str.substr(-3,3); //ipt
String 对象的方法 slice()、substring() 和 substr() (ECMAscript 没有对该方法进行标准化,因此反对使用它。)都可返回字符串的指定部分。slice() 比 substring() 要灵活一些,因为它允许使用负数作为参数。slice() 与 substr() 有所不同,因为它用两个字符的位置来指定子串,而 substr() 则用字符位置和长度来指定子串。
改变字符串展示方式(将String以特定标签包裹,输出为纯html)
String.prototype.toLowerCase 转为小写
String.prototype.toUpperCase 转为大写
String.prototype.toLocaleUpperCase 转化为本地大写,只有几种语言(如土耳其语)具有地方特有的大小写映射,所有该方法的返回值通常与 toUpperCase() 一样。
String.prototype.toLocaleLowerCase 同上
String.prototype.trim
var str = ' java ja ';
str.trim(); //java ja
String.prototype.endsWith--判断是否是以某个字串结尾
var str = 'javascript';
str.endsWith('pt'); //true
str.endsWith('script'); //true
str.endsWith('java'); //false
str.endsWith('java',4); //true
String.prototype.startsWith--判断是否是以某个字串开头,用法同上
String.prototype.includes--判断一个字符串是否被包含在另一个字符串中
var str = 'javascript';
str.includes('java'); //true
str.includes('java',5); //false
String.prototype.normalize--按照指定的一种 Unicode 正规形式将当前字符串正规化
var str = "\u6211\u662f\u8c01";
str.normalize("NFC");
str.normalize("NFD");
str.normalize("NFKC");
str.normalize("NFKD");
String.prototype.repeat--重复当前字符串若干次数的新字符串
var str = 'javascript';
str.repeat(3); //"javascriptjavascriptjavascript"
str.repeat(-1); // Uncaught RangeError: Invalid count value
str.repeat(0); //""
String.prototype[@@iterator]() -- 返回一个迭代器
var str = 'javascript';
var strIter = str[Symbol.iterator]();
strIter.next().value //j
strIter.next().value //a
strIter.next().value //v
...
strIter.next().value //t
strIter.next().value // undefined
String.raw -- String 构造函数上的静态方法,获取一个模板字符串的原始字面量值的。
String.raw 'Hi\n!'; //'Hi\n!' \n并没有起到换行的作用
var name = 'Bob';
String.raw `Hi\n${name}!`; //"Hi\nBob!"
String.raw({raw: "test"}, 0, 1, 2); //"t0e1s2t"
String.raw({raw: "testa"}, 0, 1, 2, 5); //"t0e1s2t5a"
String.raw({raw: "testa"}, 0, 1, 2); //"t0e1s2ta"
模板字符串
换行
普通字符串
var str = 'This is first line;\nThis is second line.'
模板字符串
`string text line 1
string text line 2`
占位符表达式
var user = {
name:'Bob',
age:20,
say:function(who){
return "Hello!" + who;
}
};
var str = `My name is ${user.name},I'm ${user.age} years old.I want to say ${user.say('Jerry')}`
//"My name is Bob,I'm 20 years old.I want to say Hello!Jerry"
链接字符串,以前是这样做的
var a = 1,b=3;
str = "a + b = "+(a+b);
模板字符串可以这样
str = `a + b = ${a+b}`