今天弄了个银行卡识别功能,回显的时候想要将银行卡号每四位加一个空格,这样核对卡号会方便很多,这里记录一下

1.正则表达式实现:

       

String bankCard = authStatus.getBankCard();
String bankCard = "1234567890";
String regex = "(.{4})";
bankCard = bankCard.replaceAll(regex,"$1 ");
System.out.println(bankCard);

// 打印1234 5678 90

2.java代码实现:

       

String bankCard1 = "333434343443434344";
char[] bankArray = bankCard1.toCharArray();
String bankString = "";
for(int i=0;i<bankArray.length;i++){
if(i%4==0 && i>0){
bankString +=" ";
}
bankString += bankArray[i];
}
System.out.println(bankString);

// 打印结果3334 3434 3443 4343 44

 
————————————————