node字符串处理/替换字符串 Map创建 字符串数组判断是否包括字符串 node判空

判空

在node中,可以通过下面的方式判空:

if (a)

此时如果a为null或者undefined,则a的值相当于false,如果a存在值,非空,则a的值相当于true。

测试程序

    let a;
    console.log(a === null)
    console.log(a === undefined)
    if (a){
        console.log("a不为空")
    }
    if (!a){
        console.log("a为空")
    }
    let b = null
    console.log(b === null)
    console.log(b === undefined)
    if (b){
        console.log("b不为空")
    }
    if (!b){
        console.log("b为空")
    }

测试结果

false 
true
a为空
true
false
b为空

String

字符串的替换,包括替换单个字符串中的某个字符串,替换多个字符串中的某个字符串。

replace

替换匹配的第一个字符串

	let resourcesInf = "com.android.support:appcompat-v7:28.0.0".replace(".","_").replace(":","---").split("---");
	console.log(resourcesInf)

输出:

[ 'com_android.support', 'appcompat-v7:28.0.0' ]

replaceAll

	let resourcesInf = "com.android.support:appcompat-v7:28.0.0".replaceAll(".","_").replaceAll(":","---").split("---");
	console.log(resourcesInf)

输出:

[ 'com_android_support', 'appcompat-v7', '28_0_0' ]

node低版本中replaceAll不适用

此时,如果进行批量替换时,需要使用正则的方式进行替换。
替换格式如下:

/待替换字符/g
如果代替换字符串为特殊字符,则需要使用转义字符\

let resourcesInf = "com.android.support:appcompat-v7:28.0.0".replace(/\./g, "_").replace(/\:/g, "---").split("---");

Map

遍历

var myMap = new Map();
myMap.set(0, "zero");
myMap.set(1, "one");

for (var key of myMap.keys()) {
  console.log(key);
}
// Will show 2 logs; first with "0" and second with "1"

for (var value of myMap.values()) {
  console.log(value);
}
// Will show 2 logs; first with "zero" and second with "one"

for (var item of myMap.entries()) {
  console.log(item[0] + " = " + item[1]);
}
// Will show 2 logs; first with "0 = zero" and second with "1 = one"

myMap.forEach(function(value, key) {
  console.log(key + " = " + value);
}, myMap)
// Will show 2 logs; first with "0 = zero" and second with "1 = one"
	let publicResource = new Map();
	let resourcesInf = "com.android.support:appcompat-v7:28.0.0".replaceAll(".","_").replaceAll(":","---").split("---");
	console.log(resourcesInf)
	publicResource.set(resourcesInf[0]+"---"+resourcesInf[1],resourcesInf[2])
	console.log(publicResource)

	publicResource.forEach(function (value, key){
		console.log(key)
		console.log(value)
	})
	console.log('11111')

输出:

[ 'com_android_support', 'appcompat-v7', '28_0_0' ] 
Map(1) { 'com_android_support---appcompat-v7' => '28_0_0' } 
com_android_support---appcompat-v7
28_0_0
11111

String Array

使用indexOf判断,如果字符串数组不包括字符串,则返回-1;包括返回index。

targetPermissionList.indexOf(resourceUsePermissionJson[i][attrPre + 'android:name']) === -1
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

学知识拯救世界

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值