字符串的常用方法有哪些?
参考答案
查找
indexOf/lastIndexOf、includes、startsWith/endsWith、search、match/matchAll
截取
slice(start, end)(支持负)、substring(自动交换、负值当 0)、substr(已废弃)、charAt、at(支持负)
修改(返回新串)
replace、replaceAll、toUpperCase/toLowerCase、trim/trimStart/trimEnd、concat、repeat、padStart/padEnd
分割与转换
split、normalize、localeCompare
其他
length、codePointAt、fromCharCode/fromCodePoint、String.raw
要点:字符串不可变,所有修改都返回新串;slice 与 substring 对负值处理不同;emoji 处理需按码点/字素簇([...str]、Intl.Segmenter)。