JavaScript中字符串(string)对象的常见操作包括长度计算、查找、替换、拆分、拼接等操作。
1.计算字符串长度:使用length属性可以获取字符串的长度,例如:
let str = 'Hello World'; let len = str.length; console.log(len); // 11
2.查找字符串:使用indexOf()方法可以查找字符串中指定的子字符串,例如:
let str = 'Hello World'; let index = str.indexOf('World'); console.log(index); // 6
3.替换字符串:使用replace()方法可以替换字符串中指定的子字符串,例如:
let str = 'Hello World'; let newStr = str.replace('World', 'JavaScript'); console.log(newStr); // Hello JavaScript
4.拆分字符串:使用split()方法可以拆分字符串,将字符串拆分为字符串数组,例如:
let str = 'Hello World'; let arr = str.split(' '); console.log(arr); // ['Hello', 'World']
5.拼接字符串:使用concat()方法可以拼接多个字符串,例如:
let str1 = 'Hello'; let str2 = 'World'; let str3 = str1.concat(' ', str2); console.log(str3); // Hello World
以上是JavaScript中字符串(string)对象的常见操作,可以更方便地处理字符串。