20、字符串的常见属性和方法

1.charAt() 返回在指定位置的字符,相当于数组的下标

  1. "abcdef".charAt(0); 

2.concat() 连接字符串(和数组一样)

console.log("dfa".concat("郭丹丹"));

3.indexOf() 检索某个子字符串在整个字符串中的定位

 console.log("dhsjadf".indexOf("adf"));

4.replace() 替换

console.log("abcdefgaa".replace("a","郭丹丹"))

5、slice() 提取(和数组一样)

 console.log("我爱你亲爱的祖国的人民币".slice(-3,-1) );

6、split() 把字符串→数组,  从什么地方拆分, 就是参数

  1. "我爱你亲爱祖国人民币".split("的"); 

拆开的是字符串:

  1. 1嘻嘻2嘻嘻3嘻嘻4嘻嘻5嘻嘻6".split("嘻嘻")

7.substr() 截取子串 

“字符串”.substr(start,length)

  1. "abcdefghijklmn".substr(3,5);

  从下标为3的地方开始,取5个字符

8、substring() 截取子串

“字符串”.substring(start,end); 不包括end

  1. "abcdefghijklmn".substring(3,5);

substr和substring不一样:

  1. "我爱你亲爱的祖国的人民币".substr(6,2)

  1. "我爱你亲爱的祖国的人民币".substring(6,8)

 

猜你喜欢

转载自blog.csdn.net/sinat_36414515/article/details/81382487