React Native字符串常见操作

1.字符串比较

1.判断数字相等用==
let a = 1;
        if(a==1){

        }

2.判断字符串相等用===
let b = 'iloveyou';
        if(b === 'iloveyou'){

        }

2.字符串间隔符号拆分转数组

let data = '00 11 22 33 44 55 66 77';
//拆分数据
let dataArray = data.split(' ');
//dataArray的结果为['00', '11', '22', '33', '44', '55', '66', '77'];

3.字符串截取

let data = '00 11 22 33 44 55 66 77';
//拆分数据
let dataArray = data.split(' ');
//dataArray的结果为['23', '11', '22', '33', '44', '55', '66', '77'];
let typeValue = dataArray[0].substr(0, 1);从位置0开始,截取长度为1
//结果值为2

4.字符串拼接

let array = ['00', '11', '22', '33', '44', '55', '66', '77'];
        let str = array.join('-');
//拼接的结果,str='00-11-22-33-44-55-66-77'

5.判断是否包含某个字符串

let a = 'www.baidu.com';
//判断字符串中是否包含'baidu',如果包含返回index,不包含返回-1
if(a.indexOf('baidu') == -1){
      Alert.alert('不包含', '不包含');
}else{
    Alert.alert('包含','包含位置:'+a.indexOf('baidu'));
}

6.替换字符串

let a = 'www.baidu.com';
        while(a.indexOf('w') != -1){
            a = a.replace('w', "度娘");
        }

7.字符串中数字的个数

numCnt = str.replace(/\D/g, '').length;

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值