1、length 获取字符串长度

  1. var str = "hello world";
  2. console.log(str.length); // 11 空格也算

2、.slice(startIndex,endIndex) (查询)

从哪一个下标开始,到某一个元素结束

  1. var str = "hello world";
  2. console.log(str.slice(0,8)); // hello wo 算空格

3、substr(index,howmany) (查询)

从下标值开始,截取多少位

  1. var str ="hello world"
  2. console.log(str.substr(0,4)) //hell
  3. console.log(str.substring(0,3)) //hel

4、substring(startIndex,endIndex) 和slice作用一样 (查询)

5、concat 字符串拼接(增加)

  1. var str = "hello";
  2. var s = "world";
  3. console.log(str.concat(s)); // helloworld

6、chatAt(查询)

输出某一位下标值的字符

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

7、indexOf(查询)

查询字符串值的下标 如果没有返回-1

  1. var str = "hello";
  2. console.log(str.indexOf("h")) // 0

8、 includes (查询)

字符串是否包含某位(多位)字符

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

9、split

分割字符串

  1. var str = "hello";
  2. var arr = str.split("");
  3. console.log(str.split()) // ["hello"]
  4. console.log(arr); // ["h","e","l","l","o"]
  5. console.log(str.split("l")); // ["he","","o"]

10、search

返回值的下标 没有返回-1 和indexOf类似

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

11、match

返回匹配的数组

  1. var str = "hello";
  2. var arr = str.match("l");
  3. console.log(arr); //["l", index: 2, input: "hello", groups: undefined]

12、replace

可以替换字符

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