String 对象
·
Yin灏
子字符串的索引值
let sentence = 'I hope you could live happily ever after.';
console.log(sentence.length);
console.log('');
with(console) {
log(sentence.indexOf('happy')); // -1
log(sentence.indexOf('happily')); // 22 包括空格
log(sentence.indexOf('live')); // 17
console.log('');
log(sentence.indexOf('ou')); // 8
log(sentence.indexOf('ou', 10)); // 12 从第 10 个字符开始查找
log(sentence.indexOf('ou', 14)); // -1
log(sentence.lastIndexOf('l')); // 27 反向查找
log(sentence.lastIndexOf('l', 26)); // 17
log(sentence.lastIndexOf('l', 16)); // 14
log(sentence.lastIndexOf('l', 13)); // -1
}
特定模式的子字符串的搜索
let saying = 'Joanna lovingly loves lovely beloved of Jason.';
let re01 = /lov\w+/g;
let index = saying.search(re01); // .search 只能获取第一个符合条件的字符的索引
console.log(index); // 7
console.log('');
// 获取全部符合条件的字符
console.log(saying.match(re01)); // ["lovingly", "loves", "lovely", "loved"]
console.log(saying.match(re01).length);
console.log(saying.startsWith('Jo')); // true
console.log('');
console.log(saying.startsWith('lov')); // false
console.log(saying.startsWith('lov', saying.indexOf('lovingly'))); // true
console.log('');
console.log(saying.endsWith('ed')); // false
console.log(saying.endsWith('on.')); // true
// 判断字符串中是否含有某个字符串
console.log(saying.includes('Jason')) // true
子字符串的获取
let saying = 'Do one thing at a time, and do well.';
let piece01 = saying.slice(0, 12);
let piece02 = saying.substring(0, 12);
let piece03 = saying.substr(0, 12);
console.log(piece01); // Do one thing
console.log(piece02); // Do one thing
console.log(piece03); // Do one thing
console.log('');
let start = saying.indexOf(',');
let end = saying.indexOf('.');
piece01 = saying.slice(start + 2, -1);
piece02 = saying.substring(start + 2, end);
piece03 = saying.substr(start + 2, end - start - 2);
console.log(piece01); // and do well
console.log(piece02); // and do well
console.log(piece03); // and do well
console.log('');
piece01 = saying.slice(start + 2);
piece02 = saying.substring(start + 2);
piece03 = saying.substr(start + 2);
console.log(piece01); // and do well.
console.log(piece02); // and do well.
console.log(piece03); // and do well.
console.log('');
piece01 = saying.slice(-5, -1);
piece02 = saying.substring(31, 35);
piece03 = saying.substr(31, 4);
console.log(piece01); // well
console.log(piece02); // well
console.log(piece03); // well
子字符串的替换
let str01 = ' Dread can trap you, but optimism can release you. ';
// 将至少两个连续空白字符替换为单个空白字符
let sentence = str01.replace(/\s{2,}/g, ' ');
console.log(sentence); // Dread can trap you, but optimism can release you.
// 去除开头和结尾的空格
sentence = sentence.replace(/^\s|\s$/g, '');
console.log(sentence); // Dread can trap you, but optimism can release you.
// let re01 = /(Dread)[a-z ]+(optimism)/;
// 将字符串分为 5 份,存在 5 个 () 中
let re01 = /(D)(read)([\w\s,]+)(o)(ptimism)/;
// $1, $2, $3, $4, $5 分别表示上面 5 个 () 里面的内容
sentence = sentence.replace(re01, 'o$5$3d$2');
console.log(sentence); // optimism can trap you, but dread can release you.
/*
\w 代表 1 个 a~z、A~Z、0~9、下划线字符
\s 代表 1 个空白字符
, 代表一个逗号字符
[\w\s,]+ 代表至少一个上面的字符
*/
字符串的大小写
let str01 = 'nelson';
let name01 = str01[0].toUpperCase() + str01.slice(1);
console.log(str01); // nelson
console.log(name01); // Nelson
console.log('');
let str02 = 'ALEXANDER';
let name02 = str02[0] + str02.slice(1).toLowerCase();
console.log(str02); // ALEXANDER
console.log(name02); // Alexander
不同字符串的连接
let prefix = 'Lucky numbers:';
let numbers = Array.from(new Array(6), () => parseInt(100 * Math.random()) + ' ');
console.log(numbers); // ["16 ", "63 ", "48 ", "68 ", "2 ", "33 "]
let message = prefix.concat(...numbers);
console.log(message); // Lucky numbers:16 63 48 68 2 33
message = prefix + numbers.join('');
console.log(message); // Lucky numbers:16 63 48 68 2 33
字符串的重复连接
let space = ' ';
let comma = ',';
let name = 'Alex';
let greeting = 'how are you today?';
let smile = '=^.^=';
let sentence = name + comma + space + greeting + space.repeat(5) + smile;
console.log(sentence); // Alex, how are you today? =^.^=
字符串的分割
let str01 = '10,20,50,40,70,90,80,30,60';
let str02 = ' 10, 20 , 50 ,40 , 70 , 90, 80 , 30 , 60 ';
let arr01 = str01.split(',');
console.log(arr01); // ["10", "20", "50", "40", "70", "90", "80", "30", "60"]
// 获取数组前 5 个元素
arr01 = str01.split(',', 5);
console.log(arr01); // ["10", "20", "50", "40", "70"]
let arr02 = str02.replace(/ /g, '');
console.log(arr02); // 10,20,50,40,70,90,80,30,60
arr02 = arr02.split(',');
console.log(arr02); // ["10", "20", "50", "40", "70", "90", "80", "30", "60"]
// 去除首尾空格
arr02 = str02.replace(/^\s*|\s*$/g, '');
console.log(arr02); // 10, 20 , 50 ,40 , 70 , 90, 80 , 30 , 60
arr02 = arr02.split(/\s*,\s*/);
console.log(arr02); // ["10", "20", "50", "40", "70", "90", "80", "30", "60"]
特定字符和Unicode 数码的双向转换
无论英文字母、数字、符号等字符,还是中文,均可以对应到 Unicode
字符集里面的特定编码。
let name = '柯霖廷';
let str = 'abcdefg';
// 返回字符在 utf-16 编码单元中的数码
console.log(name.charCodeAt(0)); // 26607
// 返回字符在 unicode 编码单元中的数码
console.log(name.codePointAt(0)); // 26607
console.log('');
console.log(name[0]); // 柯
console.log(name.charAt(0)); // 柯
console.log(String.fromCodePoint(26607)); // 柯
console.log(String.fromCharCode(26607)); // 柯
console.log('');
console.log(str.charCodeAt(1)); // 98
console.log(str.codePointAt(1)); // 98
console.log('');
console.log(str[1]); // b
console.log(str.charAt(1)); // b
console.log(String.fromCodePoint(98)); // b
console.log(String.fromCharCode(98)); // b
重复填充子字符串与扩充后的字符串中
如果欲在扩充之后的特定字符串里,重复填充新的子字符串,可调用 String
对象实例支持的函数 pasStart()
来实现。
let word01 = 'Happy ', word02 = ' Birthday';
let num = 567;
// 17 为需要填充的总长度,word02 的原长度为 9,也就是再需要填充 8 个字符,会填充在 word02 的前面
console.log(word01 + '^v^'.repeat(3) + word02.padStart(17, '_/')); // Happy ^v^^v^^v^_/_/_/_/ Birthday
// 一共 16 个字符,需要填充 13 个 0
console.log(num.toString().padStart(16, '0')); // 0000000000000567