目录
7、str.toLocalLowerCase() 根据本地格式进行转换,转小写
8、str.toLocalUpperCase() 根据本地格式进行转换,转大写
1、str.concat()
用于将一个或多个字符串拼接起来,返回拼接后的新字符串
参数:可以有多个,用来拼接到str上的字符串
let str = 'hello';
console.log(str.concat(' ','world'))
// 打印结果:'hello world'
提示:此方法效率并不高,可用“+”或者 `` 模板字符串代替。
2、str.slice()
用来提取一个字符串,返回一个新的字符串
参数:
startIndex:开始截取的下标(包含当前下标字符);如果是负数,则从后往前截取;
endIndex:结束截取的下标(不包含当前下标字符);如果省略,则默认截取都字符串末尾;如果是负数,则从后往前计算;
let str = 'hello world';
console.log(str.slice(6))
// 打印结果:'world'
console.log(str.slice(-5,-3))
// 打印结果:'wo'
3、str.substring()
此方法和slice方法功能相同都是提取一个字符串,返回提取到的字符串
参数:
startIndex:开始截取的下标(包含当前下标字符)
endIndex:结束截取的下标(不包含当前下标字符)
提示:上述两个参数,如果为负数或者NaN,则都会被当做0处理;如果大于字符串的长度,则会被当做字符串的长度来计算;如果 startIndex 大于 endIndex,则 substring 的执行效果就像两个参数调换了一样;
let str = 'hello world';
console.log(str.substring(-1,5))
// 打印结果:'hello'
console.log(str.substring(5,-1))
// 打印结果:'hello'
4、str.trim()
删除一个字符串两端的空白字符,返回删除后的新字符串,不会改变原有字符串;
5、str.toLowerCase()
没有参数,会将调用该方法的字符串值转为小写形式,并返回
6、str.toUpperCase()
没有参数,会将调用该方法的字符串值转为大写形式,并返回
7、str.toLocalLowerCase() 根据本地格式进行转换,转小写
//根据本地格式进行转换
console.log('A'.toLocaleLowerCase())
// 打印结果:a
8、str.toLocalUpperCase() 根据本地格式进行转换,转大写
//根据本地格式进行转换
console.log('a'.toLocaleUpperCase())
// 打印结果:A
9、str.replace()
可以将一个替换值替换字符串的一部分,返回替换后的新字符串
let str = 'hello world';
console.log(str.replace(/o/g,"f"))
// 打印结果:"hellf wfrld"
10、str.split()
可以使用一个指定的分隔符来将字符串拆分成数组,返回一个数组
let str = 'hello world';
console.log(str.split(" "))
// 打印结果:["hello", "world"]
11、str.charAt()
从一个字符串中返回指定下标的字符
参数:介于 0 到 length-1 之间的整数,默认为0
let str = 'hello world';
console.log(str.charAt(1))
// 打印结果:'e'
12、str.includes()
判断字符串中是否包含指定字符,包含则返回true,不包含则返回false
let str = 'hello world';
console.log(str.includes('hello'))
// 打印结果:true
console.log(str.includes('秋雅'))
// 打印结果:flase
13、str.indexOf()
判断字符串中是否包含指定字符。如果包含,则返回该字符索引的位置(查找到了立即返回);如果不包含,则返回-1。
let str = 'hello world';
console.log(str.indexOf('world'))
// 打印结果:6
console.log(str.indexOf('fire'))
// 打印结果:-1
14、str.lastIndexOf()
用法和indexOf基本相同,区别是lastIndexOf()是从后往前查找
15、str.search()
使用正则表达式查找指定字符串。如果找到,则返回首次匹配成功的索引;没有找到,则返回-1。
let str = 'hello world';
console.log(str.search('world'))
// 打印结果:6
console.log(str.search(/w/))
// 打印结果:6
16、str.match()
返回一个字符串匹配正则表达式的结果。如果未设置全局匹配,则会返回第一个完整匹配及其相关的捕获组,捕获组中包含有groups、index、input等属性。
let str = 'hello world';
console.log(str.match(/l/))
// 打印结果:["l", index: 2, input: "hello world", groups: undefined]
console.log(str.match(/l/g))
// 打印结果:["l", "l", "l"]
拓展:
1、charCodeAt()
返回的不是字符而是字符编码。
JavaScript charCodeAt() 方法-优快云博客
2、localCompare() 比较两个字符串
如果字符串在字谜表中应该排在字符串参数之前,则返回一个负数(大多数情况下是-1,具体的值要视实现而定);
如果字符串等于字符串参数,则返回0;
如果字符串在字母表中应该排在字符串参数之后,则返回一个正数(大多数情况下是1,具体的值同样要视实现而定)。
var stringValue = "yellow";
console.log(stringValue.localCompare("brick"));
// 打印结果:1;
console.log(stringValue.localCompare("yellow"));
// 打印结果:0;
console.log(stringValue.localCompare("zoo"));
// 打印结果:-1;
localCompare() 方法比较与众不同的地方,就是实现所支持的地区(国家和语言)决定了这个方法的行为,比如,美国以英语作为 ECMAScript 实现的标准语言,因此 localCompare() 方法就是区分大小写的,域是大写字母在字母表中排在小写字母前头就成为一项决定性的比较规则。不过,在其他地区恐怕就不是这种情况了。
3、fromCharCode()
接受一或多个字符编码,然后将它们将转换成一个字符串。从本质上来看,这个方法与实例方法 cherCodeAt() 执行的相反的操作。
console.log(String.formCharCode(104, 101, 108, 108, 111));
// 打印结果:"hello"