| var str = "abc123def666"; |
| // charAt() 方法返回字符串中指定位置的字符。 |
| // 参数:index |
| // console.log(str.charAt(6)); // d |
| |
| // indexOf() 方法返回 指定值 在字符串对象中首次出现的位置。 |
| // 从 fromIndex 位置开始查找,如果不存在,则返回 -1。 |
| // 注意:区分大小写 |
| // console.log(str.indexOf("123")); // 3 |
| // console.log(str.indexOf("123", 2)); // 3 |
| |
| // 正则方法 |
| // search |
| // 执行一个查找,看该字符串对象与一个正则表达式是否匹配。 |
| // console.log(str.search("1")); // 3 |
| // console.log(str.search(/[0-9]/)); // 3 |
| |
| // replace |
| // 被用来在正则表达式和字符串直接比较,然后用新的子串来替换被匹配的子串。 |
| // console.log(str.replace("1", "-one-")); |
| // console.log(str.replace(/\d/, "-one-")); |
| |
| // match |
| // 当字符串匹配到正则表达式(regular expression)时, |
| // match() 方法会提取匹配项。 |
| // console.log(str.match(/\d+/)); // ["123", index: 3, input: "abc123def666"] |
| |
| |
| // split |
| // 通过把字符串分割成子字符串来把一个 String 对象分割成一个字符串数组。 |
| // console.log(str.split("1")); // ["abc", "23def666"] |
| |
| // slice |
| // 提取字符串中的一部分,并返回这个新的字符串 |
| // 获取索引号为1,2,3的字符串,即[1, 4) |
| // console.log(str.slice(1, 4)); // bc1 |
| |
| // substr |
| // 方法返回字符串中从指定位置开始到指定长度的子字符串。 |
| // 开始位置 和 长度 |
| // console.log(str.substr(3, 3)); // 123 |
| |
| // substring |
| // 返回字符串两个索引之间(或到字符串末尾)的子串。 |
| // 开始位置 和 结束位置 |
| // console.log(str.substring(3, 6)); // 123 |
| |
| |
| // trim() |
| // 删除一个字符串两端的空白字符 |
| // str = " abc123 def666 "; |
| // console.log("|" + str.trim() + "|"); // |abc123 def666| |
| |
| // toLowerCase() |
| // 将调用该方法的字符串值转为小写形式,并返回。 |
| // console.log(str.toLowerCase()); // abc123def666 |
| |
| // toUpperCase() |
| // 将字符串转换成大写并返回。 |
| // console.log(str.toUpperCase()); // ABC123DEF666 |