增加

concat()

  1. var a = 'hello'
  2. var b = "world"
  3. console.log(a.concat(b));

截取

slice(1,4)截取字符串中的某一段从下标1到下标4
substr(1,4)从下标1开始获取4位
substring(1,4)截取字符串中的某一段从下标1到下标4
split()将字符串分割成字符串数组

  1. console.log(arr.slice(0)); // hello
  2. console.log(arr.slice(1,3)); // el
  3. console.log(arr.substr(1,3)); // ell
  4. console.log(arr.substring(1,3)); // el
  1. var str = "hello"
  2. console.log(str.split()); // 将字符串转为数组 ["hello"]
  3. console.log(str.split("")); // ["h","e","l","l","o"]
  4. console.log(str.split("e"));// ["h","llo"]

查找

charAt(index) 根据下标查找对应的值

  1. var str = "hello"
  2. console.log(str.charAt(1)); // e

indexOf(value) 根据值查找对应的下标 找不到返回-1

  1. var arr = "hello";
  2. console.log(arr.indexOf('e')); // 1

search(value) 根据值查找对应的下标 找不到返回-1

  1. var str = "你是谁"
  2. var index = str.search("她")
  3. console.log(index); // -1

includes 是否包含某位(多位)字符 返回boolean

  1. var arr = "hello"
  2. console.log(arr.includes("eh")); //false

match(value) 返回匹配的字符串,返回的是数组

  1. var str ="hello"
  2. var arr = str.match("l")
  3. console.log(arr); // ["l", index: 2, input: "hello", groups: undefined]
  4. // 找不到返回 null

**length 字符串的长度

  1. var str = "hello"
  2. console.log(str.length);
  3. var s = "故事的结尾,心上人"
  4. console.log(handleStr(s));
  5. function handleStr(value){
  6. if(value.length>5){
  7. return value.slice(0,5)+"..."
  8. }
  9. return value
  10. }

其他方法

replace()

  1. var str = "hello"
  2. console.log(str.replace("l","*")); // he*lo

trim( ) 去除字符串前后的空格
startsWith( ) 以…开头的字符串