查找
- indexOf() 查找-字符串首次首次出现位置 找到返回位置 否则返回-1 区分大小写
let search_str = "Hello world, welcome to the school.";
console.log(search_str.indexOf('world'));
console.log(search_str.indexOf('aa'));
- lastIndexOf() 查找-字符串首次尾次出现位置 找到返回位置 否则返回-1 区分大小写
console.log(search_str.lastIndexOf('e'));
- includes() 查找-子字符串 找到返回true 否则返回false 区分大小写
console.log(search_str.includes('world'));
- search() 查找-子字符串或者正则匹配 找到返回位置 否则返回-1
console.log(search_str.search('world'));
console.log(search_str.search('xz'));
console.log(search_str.match(/world/g));
- 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)
console.log(str2)
console.log(concat_str)
- repeat() 拼接-复制字符串指定次数,并将它们连接在一起返回
let copy = '1005';
console.log(copy.repeat(2));
console.log(copy);
分割
- 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));
console.log(tiqu_str.slice(-3,-1));
console.log(tiqu_str.slice(2));
console.log(tiqu_str.slice(-2),'s');
- substring(start, end) 提取-字符串[start, end) 非负数的整数为起始 省略的话直接到字符串结尾 不改变原字符串
console.log(tiqu_str.substring(1,4),'ss');
console.log(tiqu_str.substring(3,4));
console.log(tiqu_str.substring(2));
console.log(tiqu_str);
替换
- replace() 替换-用一些字符替换另一些 或替换一个与正则匹配的子串 不改变原字符串
let replace_str = "Hello world, welcome to the school. world"
console.log(replace_str);
console.log(replace_str.replace('world','newworld'));
console.log(replace_str);
转换
- toLowerCase() 字符串转小写 不改变原字符串
let bigtosmall = 'XZLP'
console.log(bigtosmall.toLowerCase())
console.log(bigtosmall)
- toUpperCase() 字符串转大写 不改变原字符串
let smalltobig = 'xzlp'
console.log(smalltobig.toUpperCase())
console.log(smalltobig)
字符
let str = "hello world"
console.log(str.charAt(1))
- charCodeAt() 返回在指定的位置的字符的 Unicode 编码
console.log(str.charCodeAt(2));
- fromCharCode() 将 Unicode 编码转为字符
let n = String.fromCharCode(65)
console.log(n)
- startsWith() 判断字符串是否以指定的子字符串开头(区分大小写)
console.log(str.startsWith('hello'));
console.log(str.startsWith('WORLD'));
- endsWith() 判断字符串是否以指定的子字符串结尾(区分大小写)
console.log(str.endsWith('world'));
console.log(str.endsWith('WORLD'));
去空
let space_str = ' x z l p '
console.log(space_str.trim())
console.log(space_str)
返回一个表示String对象的值
返回对象原始值