1.获取指定字符的前/后的值
str = "123~456"
str.split("~")[0] 输出为 123
str.split("~")[1] 输出为 456
2.获取指定位置字符
var str="Hello world!"
str.substring(3) 输出为 lo world!
str.substring(3,7) 输出为 lo w
str.slice(3,7) 输出为 lo w
str.slice(-3,-1) 输出为 ld 倒数第三个位置到倒数第一个位置
str.substr(3,6) 输出为 lo wor 第三个位置开始以后的6个字符
*与 slice() 和 substr() 方法不同的是,substring() 不接受负的参数。
3.删拼接2个字符串
var a = "he"
var b = "llo"
a.concat(b)或者 a + b 输出hello
4.删除指定位置的字符
var str="Hello world";
str = str.slice(0,3) + str.slice(4) 输出 Helo world
*str.slice(0,x) + str.slice(x+n) x代表要删除的位置,n代表删除字符的个数
5.删除最后一位字符
var s="Hello world";
s=s.substring(0,s.length-1) 输出 Hello worl
6.替换指定位置的字符
var str = "hello";
str.slice(0, 1) + "-" + str.slice(1); 输出 h-ello
7.替换 / 删除指定字符
var str="Hello world"
str.replace(/o/g,'9') 输出 Hell9 w9rld
str.replace(/o/g,'') 输出 Hell wrld
8.返回某个字符在字符串中第一处出现的索引,如果没有匹配项,返回 -1
var str="hello"
str.indexOf("l") 输出2
9.返回字符串中一个子串最后一处出现的索引,如果没有匹配项,返回 -1
var str="hello"
str.lastIndexOf("l") 输出3
10.将字符串转换成数组
var str="hello"
str.split("") 输出 ['h', 'e', 'l', 'l', 'o']
let obj = 'okl'
let b = Array.from(obj)
console.log(b) //['o', 'k', 'l']
11.字符串的长度 即 字符串的个数
var str="hello"
str.length 输出 5
12.将字符串中大写字符转换成小写
var str="Hello"
str.toLowerCase() 输出 hello
13.将字符串中小写字符转换成大写
var str="Hello"
str.toUpperCase() 输出 HELLO
14.去除字符串左右两边的空格
var str=" H ello "
str.replace(/(^\s*)|(\s*$)/g, "") 或者 str.trim() 去除左右两端的空格
str.replace(/(^\s*)/g,"") 去除左边的空格
str.replace(/(\s*$)/g,"") 去除右边的空格
str.replace(/\s/g, "") 去除所有空格
15.提取字符串中的数字,中文,英文
var str ="一共4500元aaa";
value.replace(/[^\d]/g,'') 提取数字
value.replace(/[^\u4E00-\u9FA5]/g,'') 提取中文
value.replace(/[^a-zA-Z]/g,'') 提取英文
目前只想到这些,如果有其他的操作欢迎评论~我再加上