JS-字符串相关

126 阅读2分钟

查找

  • indexOf() 查找-字符串首次首次出现位置 找到返回位置 否则返回-1 区分大小写
let search_str = "Hello world, welcome to the school.";
console.log(search_str.indexOf('world')); // 6
console.log(search_str.indexOf('aa')); // -1
  • lastIndexOf() 查找-字符串首次尾次出现位置 找到返回位置 否则返回-1 区分大小写
console.log(search_str.lastIndexOf('e')); // 26
  • includes() 查找-子字符串 找到返回true 否则返回false 区分大小写
console.log(search_str.includes('world')); // true
  • search() 查找-子字符串或者正则匹配 找到返回位置 否则返回-1
console.log(search_str.search('world')); // 6
console.log(search_str.search('xz')); // -1
console.log(search_str.match(/world/g)); // [ 'world' ]
  • match() 查找-正则匹配 一个或多个 找到返回对应的结果数组 否则返回null
let reg_str = "The rain in SPAIN stays mainly in the plain"; 
console.log(reg_str.match(/ain/g)); // [ 'ain', 'ain', 'ain' ]

拼接

  • concat() 拼接-两个或更多字符串 并返回新的字符串 不改变原字符串
let str1 = 'hello';
let str2 = 'world';
let concat_str = str1.concat(str2);
console.log(str1); // hello
console.log(str2); // world
console.log(concat_str); // helloworld
  • repeat() 拼接-复制字符串指定次数,并将它们连接在一起返回
let copy = '1005';
console.log(copy.repeat(2)); // 10051005
console.log(copy); // 1005

分割

  • split(str,limit) 分割-按指定条件/正则分割字符串 返回字符串数组 不改变原字符串
let fenge_str = "How are you doing today?";
console.log(fenge_str.split(' ')); // [ 'How', 'are', 'you', 'doing', 'today?' ]
console.log(fenge_str.split(' ',3),); // [ 'How', 'are', 'you' ]
console.log(fenge_str.split(/are/g),); // [ 'How ', ' you doing today?' ]
console.log(fenge_str.split(/are/g,1),); // [ 'How ' ]
console.log(fenge_str); // How are you doing today?

提取

  • slice(start, end) 提取-字符串[start, end) 并在新的字符串中返回被提取的部分 不改变原字符串
let tiqu_str = "How are you doing today?";
console.log(tiqu_str.slice(1,4)); // ow 从0开始
console.log(tiqu_str.slice(-3,-1)); // ay 从-1开始
console.log(tiqu_str.slice(2)); // w are you doing today? 从2开始到结尾
console.log(tiqu_str.slice(-2),'s'); // y? 从-2开始到结尾
  • substring(start, end) 提取-字符串[start, end) 非负数的整数为起始 省略的话直接到字符串结尾 不改变原字符串
console.log(tiqu_str.substring(1,4),'ss'); // ow
console.log(tiqu_str.substring(3,4));// w are you doing today?
console.log(tiqu_str.substring(2));  // How are you doing today?
console.log(tiqu_str); // How are you doing today?

替换

  • replace() 替换-用一些字符替换另一些 或替换一个与正则匹配的子串 不改变原字符串
let replace_str = "Hello world, welcome to the school. world"
console.log(replace_str); // Hello world, welcome to the school. world
console.log(replace_str.replace('world','newworld')); // Hello newworld, welcome to the school. world
console.log(replace_str); // Hello world, welcome to the school. world

转换

  • toLowerCase() 字符串转小写 不改变原字符串
let bigtosmall = 'XZLP';
console.log(bigtosmall.toLowerCase()); // xzlp
console.log(bigtosmall); // XZLP
  • toUpperCase() 字符串转大写 不改变原字符串
let smalltobig = 'xzlp';
console.log(smalltobig.toUpperCase()); // XZLP
console.log(smalltobig); // xzlp

字符

  • charAt() 返回在指定位置的字符
let str = "hello world";
console.log(str.charAt(1)); //e
  • charCodeAt() 返回在指定的位置的字符的 Unicode 编码
console.log(str.charCodeAt(2)); //108
  • fromCharCode() 将 Unicode 编码转为字符
let n = String.fromCharCode(65);
console.log(n); // A
  • startsWith() 判断字符串是否以指定的子字符串开头(区分大小写)
console.log(str.startsWith('hello')); // true
console.log(str.startsWith('WORLD')); // false
  • endsWith() 判断字符串是否以指定的子字符串结尾(区分大小写)
console.log(str.endsWith('world')); // true
console.log(str.endsWith('WORLD')); // false

去空

  • trim() 去头尾空 不改变原始值
let space_str = ' x z l p ';
console.log(space_str.trim()); // x z l p
console.log(space_str); // x z l p 

返回一个表示String对象的值

返回对象原始值