Java字符串每四位隔一个空格

在项目中,想把银行卡号每隔4位加一个或若干个空格,不管它的长度的实现方法:

1.正则表达式实现:

        String bankCard = authStatus.getBankCard();
        String regex = "(.{4})";
        bankCard = bankCard.replaceAll(regex,"$1\t\t");
        mTxtBankCard.setText(bankCard);


2.java代码实现:

        String bankCard = authStatus.getBankCard();
        char[] bankArray = bankCard.toCharArray();
        String bankString = "";
        for(int i=0;i0){
                bankString +=" ";
            }
            bankString += bankArray[i];
        }
        mTxtBankCard.setText(bankString);


你可能感兴趣的:(Java)