javascript中string字符串的各种方法

1.charAt() 返回指定位置的字符串,空格占位

var txt='123456';
console.log(txt.charAt(5));

2.replace() 在字符串中用一些字符去替代另一些字符

var txt="hello world hello hello";
txt=txt.replace('hello','happy');
console.log(txt)

3.split() 把一个字符串分割成字符串数组

var txt="hello world";
console.log(txt.split(""));

4.indexOf() 返回某个指定的字符串,在字符串中首次出现的位置,没有返回-1

var txt="hello world";
console.log(txt.indexOf('hello'));
console.log(txt.indexOf('world'));
console.log(txt.indexOf('Wiorld'));

5.lastIndexOf() 返回某个指定的字符串在字符串中最后出现的位置,没有返回-1

var txt="hello world hello world";
console.log(txt.lastIndexOf('hello'));
console.log(txt.lastIndexOf('world'));
console.log(txt.lastIndexOf('Wiorld'));

6.match()  匹配,在字符串中检索指定的值,查不到null

var txt="hello world hello world";
console.log(txt.match('hello'));
console.log(txt.match('world'));

7.toLowerCase()转换为小写
8.toUpperCase()转化为大写

var txt="hello world HELLO world";
console.log(txt.toLocaleLowerCase());
console.log(txt.toUpperCase());

9.slice() 数组和字符串都能用,提取某个字符串的某个部分,并以新的字符串返回被提取的部分

var txt="hello world HELLO world";
console.log(txt.slice(3,7))

10.substr() 从索引号提取指定数目的字符,(开始位置,,提取数量)

var txt="hello world HELLO world";
console.log(txt.substr(3,7))

11. substring,同9,>=start,<end

12.trim 删除前置和后置的空格

var txt="hello world HELLO world";
console.log(txt);

13.number对象里面的toFixed(),保留两位小数

var aa=31.2141;
console.log(aa.toFixed(2));

14.search()方法,检索字符串,指定的字符串,或者是说,检索与正则表达式匹配的字符串

var txt="hello world HELLO world";
document.write(txt.search(/world/));//6
document.write(txt.search(/World/));//6 i执行大小写不敏感的匹配

15. localCompare -1 0 1
括号里面的是目标值
如果str和目标值去比,如果str<目标值,返回小于0的数-1
如果str和目标值去比,如果str=目标值,返回0
如果str和目标值去比,如果str>目标值,返回

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值