前端字符串常用属性和方法

length---字符串长度

var abc="helloworld"
console.log(abc.length)  // 输出10

charAt(索引)---返回值是指定索引位置的字符串,超出则为空

var abc="helloworld"
console.log(abc.charAt(5))  // 输出 w

fromCharCode(数字值,可多个参数)---返回ASCII码对应的值

console.log(String.fromCharCode(65))  //输出 A

concat(字符串1,字符串2,···)---拼接字符串,返回新的字符串

var abc="hello"
var def="world"
console.log(abc.concat(def))

indexOf(要找的字符串,从某个位置开始的索引)---返回这个字符串的索引值,没找到返回-1;

lastIndexOf(要找的字符串)---从后找

var abc="helloworld"
console.log(abc.indexOf('world'))  // 输出 5 

replace('原来的字符串','新的字符串') ---替换字符

var abc="helloworld"
console.log(abc.replace('world','Dandelion')) //输出 helloDandelion

slice(开始的索引,结束的索引)---返回提取的字符串

var abc="helloworld"
console.log(abc.slice(3,7)) //输出:lowo

split(要去掉的字符,切割后留下的个数)---切割字符串

var abc="aaaa?bbbb?cccc?dddd?eeee"
console.log(abc.split('?',3)) //输出:['aaaa', 'bbbb', 'cccc']

substr(开始的位置,个数)---返回截取后的新字符串(包括结束索引)

var abc="helloworld"
console.log(abc.substr(5,9)) //输出:world

substring(开始的索引,结束的索引)---返回截取后的字符串(不包括结束索引)

var abc="helloworld"
console.log(abc.substring(1,4)) //输出:ell

toLocaleLowerCase() 和 toLowerCase(),转换为小写

var abc="HELLOworld"
console.log(abc.toLocaleLowerCase()) //输出:helloworld
console.log(abc.toLowerCase()) //输出:helloworld

toLocaleUpperCase() 和 toUpperCase(),转换为大写

var abc="HELLOworld"
console.log(abc.toLocaleUpperCase()) //输出:HELLOWORLD
console.log(abc.toUpperCase()) //输出:HELLOWORLD

trim() ---去掉字符串两端的空格

var abc="   hello   world   "
console.log('w'+abc.trim()+'w') //输出:whello   worldw

小带一笔:

toFixed() ⽅法可把 Number 四舍五⼊为指定⼩数位数的数字。语法是:数字.toFixed(num);其中num必需,指规定⼩数的位数,是 0 ~ 20 之间的值,包括 0 和 20,有些实现可以⽀持更⼤的数值范围。如果省略了该参数,将⽤ 0 代替。


本文来自互联网用户投稿,文章观点仅代表作者本人,不代表本站立场,不承担相关法律责任。如若转载,请注明出处。 如若内容造成侵权/违法违规/事实不符,请点击【内容举报】进行投诉反馈!

相关文章

立即
投稿

微信公众账号

微信扫一扫加关注

返回
顶部