JS_字符串操作

var str = 'hello world'
// 获取字符串长度
console.log(str.length)
// 获取字符串指定位置的字符
console.log(str.charAt('4'))
// 获取字符串指定位置字符的字符编码(Unicode)
console.log(str.charCodeAt(4))
// 将字符串依次拼接在后面
console.log(str.concat('wdc', 'yhf'))
// 获取字符串下标为2开始到4之前的字符或字符串。(参数可以为负值)
console.log(str.slice(2,4))
console.log(str.substring(2,4))
// 获取字符串下标为2开始向后的6个字符的字符串
console.log(str.substr(2,6))

 结果:

原文地址:https://www.cnblogs.com/wangdianchao/p/12609633.html