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;