欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

Javascript中字符串相关常用的使用方法总结

程序员文章站 2023-10-10 18:55:28
刚刚在看javascript犀牛书,看到字符串这一节,平时工作接触到这方面的不多,想着整理下,以备不时只需。 javascript的内置功能之一就是字符串连接,如果用‘+...

刚刚在看javascript犀牛书,看到字符串这一节,平时工作接触到这方面的不多,想着整理下,以备不时只需。

javascript的内置功能之一就是字符串连接,如果用‘+'连接两个数字,表示两数相加。但是如果用于字符串,就表示第二个字符加在第一个字符之后。

var num=1+2;
console.log(num);
var msg='hello'+'world';
console.log(msg);

对于字符串来说,除了有length属性,还有很多其他有用的属性,如:

var str='hello,world';
console.log(str.length);
console.log(str.charat(0)); //第一个字符 
console.log(str.charat(str.length-1)); // 最后一个字符 
//substring(starti,endi), 截取 字符串 起始startistartii, 到endi结束,且含头不含尾,没有
// 如果第二个参数没有, 就默认截取到最后一个。  
console.log(str.substring(1,4));
console.log(str.substring(1));
//用法同上,为负数时,就是倒数开始算,一个参数含义是倒数几个。
console.log(str.slice(1,4));
console.log(str.slice(-3));
//字符‘l'首次出现的位置 
console.log(str.indexof('l'));
//字符‘l'最后一次出现的位置 
console.log(str.lastindexof('l'));
//在位置下标3之后,首次出现的位置 
console.log(str.indexof('l',3));
//用‘,'分割为字符串
console.log(str.split(','));
// 把str中的小写h换成大写h
console.log(str.replace('h','h'));
//讲字符串转化为大写
console.log(str.touppercase());

提示:在javascript中字符串本身是固定不变的,上述方法都会返回一个新的字符串值,并不会影响str本身的值

值得注意的是,在es6中,给字符串增加了很多新的方法,如:

var s = 'hello world!';
// 返回布尔值,表示参数字符串是否在源字符串中的头部
console.log(s.startswith('hello')) // true
//endswith():返回布尔值,表示参数字符串是否在源字符串的尾部
console.log(s.endswith('!')) // true
// includes()返回布尔值,表示是否找到了参数字符串
console.log(s.includes('o')) // true

上述的三个方法都支持第二个参数,表示开始搜索的位置

var s = 'hello world!';
console.log(s.startswith('world', 6)) // true
console.log(s.endswith('hello', 5))// true
console.log(s.includes('hello', 6)) // false

提示:endswith的行为与其他两个方法有所不同。它针对前n个字符,而其他两个方法针对从第n个位置直到字符串结束。
repeat方法返回一个新字符串,表示将原字符串重复n次。

console.log('x'.repeat(3)) // "xxx"
console.log('hello'.repeat(2)) // "hellohello"
console.log('na'.repeat(0)) // ""

参数如果是小数,会被取整。

console.log('na'.repeat(2.9)) // "nana"
//如果repeat的参数是负数或者infinity,会报错。
console.log('na'.repeat(infinity))
// rangeerror
console.log('na'.repeat(-1))
// rangeerror

但是,如果参数是0到-1之间的小数,则等同于0,这是因为会先进行取整运算。0到-1之间的小数,取整以后等于-0,repeat视同为0。

console.log('na'.repeat(-0.9)) // ""
//参数nan等同于0
console.log('na'.repeat(nan)) // "
//如果repeat的参数是字符串,则会先转换成数字
console.log('na'.repeat('na')) // ""
console.log('na'.repeat('3')) // "nanana"

es2017 引入了字符串补全长度的功能。如果某个字符串不够指定长度,会在头部或尾部补全。padstart()用于头部补全,padend()用于尾部补全

//padstart和padend一共接受两个参数,第一个参数用来指定字符串的最小长度,第二个参数是用来补全的字符串。
console.log('x'.padstart(5, 'ab')) // 'ababx'
console.log('x'.padstart(4, 'ab')) // 'abax'
console.log('x'.padend(5, 'ab')) // 'xabab'
console.log('x'.padend(4, 'ab')) // 'xaba'
//如果原字符串的长度,等于或大于指定的最小长度,则返回原字符串。
console.log('xxx'.padstart(2, 'ab')) // 'xxx'
console.log('xxx'.padend(2, 'ab')) // 'xxx'
//如果用来补全的字符串与原字符串,两者的长度之和超过了指定的最小长度,则会截去超出位数的补全字符串。
consoe.log('abc'.padstart(10, '0123456789'))
// '0123456abc'
//如果省略第二个参数,默认使用空格补全长度。
console.log('x'.padstart(4)) // ' x'
console.log('x'.padend(4)) // 'x '
//padstart的常见用途是为数值补全指定位数。下面代码生成10位的数值字符串。
console.log('1'.padstart(10, '0') )// "0000000001"
console.log('12'.padstart(10, '0')) // "0000000012"
console.log('123456'.padstart(10, '0')) // "0000123456"
//另一个用途是提示字符串格式。
console.log('12'.padstart(10, 'yyyy-mm-dd')) // "yyyy-mm-12"
console.log('09-12'.padstart(10, 'yyyy-mm-dd'))// "yyyy-09-12"

以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,同时也希望多多支持!