我想用Ionic-4屏蔽一个字符串,字符串的格式是1234567890987。我希望输出为123xxxxxx987(即)前3个字符,最后3个字符应该是简单的,其余的所有字符都应该被屏蔽。请告诉我,如何实现这一点?
发布于 2018-11-15 07:41:14
定义一个函数replaceAt如下-
function replaceAt(str, pos, value){
var arr = str.split('');
arr[pos]=value;
return arr.join('');
}
var s = "1234567890987";
for(var i=3;i<s.length-3;i++) s = replaceAt(s, i, 'x');
console.log(s);
发布于 2018-11-15 07:37:17
你可以这样做:
const number:string = "123456789";
const hideMiddleString = (text: string): string => {
if(text.length <= 6) {
return text;
}
const beginString = text.substr(0, 3); // Take 3first chars
const endString = text.substr(-3); // take 3 last chars
// x.repeat will create string of xxxx base on string length - 3 first chars - 3 last chars
return beginString + "x".repeat(text.length - 6) + endString;
};
console.log(hideMiddleString(number));https://stackoverflow.com/questions/53314211
复制相似问题