一 字符串转换
// int to string
var n = 1;
n.toString();
String(n);
'' + n
// string to int
var s = '123a';
Number(s);
parseInt(s); // 123 十进制
parseInt(s, 16); // 4666 十六进制
parseFloat(s); // 123
s = '123'; s - 0;
二 字符串分割
var s = 'abcdef-123456';
s.split('-'); // ['abcdef', '123456']
s.split('-', 1); // ['abcdef']
三 字符串查找
var s = 'abcdef';
s.indexOf('e'); // 4
s.lastIndexOf('e'); // 4
s.charAt(4); // e
四 字符串替换
var s = 'abcabc';
s.replace('a', '1'); // "1bcabc"
s.replace(/a/g, '1'); // "1bc1bc"
五 字符串匹配
var s = 'abcdef-123456';
s.match('ef'); // ["ef", index: 4, input: "abcdef-123456", ...]
s.match(/\w+/); // ["abcdef", index: 0, input: "abcdef-123456", ...]
s.match(/\w+/g); // ["abcdef", "123456"]
s.search(/\d+/); // 7
var reg = /\w+/;
reg.exec(s); // ["abcdef", index: 0, input: "abcdef-123456", ...]
六 字符串切割
var s = 'abcdef-123456';
s.substr(1, 4); // "bcde"
s.substring(1, 4); // "bcd"
s.slice(-3, -1); // "45"
七 大小写转换
var s = 'abcDEF-123456';
s.toLowerCase(); // "abcdef-123456"
s.toUpperCase(); // "ABCDEF-123456"
八 去除空格
var s = ' abc ';
s.trim(); // "abc"
九 附录:字符串对象的所有方法
length
anchor()
big()
blink()
bold()
charAt()
charCodeAt()
concat()
constructor()
endsWith()
fixed()
fontcolor()
fontsize()
includes()
indexOf()
italics()
lastIndexOf()
link()
match()
matchAll()
normalize()
padEnd()
padStart()
repeat()
replace()
search()
slice()
small()
split()
startsWith()
strike()
sub()
substr()
substring()
sup()
toLocaleLowerCase()
toLocaleUpperCase()
toLowerCase()
toString()
toUpperCase()
trim()
trimEnd()
trimLeft()
trimRight()
trimStart()
valueOf()
hasOwnProperty()
isPrototypeOf()