在这种情况下,我无法使用数组。


如果我做:

字符串地址=“ 100 Point St Apt B”

它也应该被屏蔽:100 Po*** St Apt *
如果我做:

字符串地址=“ 100 Point St Apt 132”

它也被掩盖:100 Po*** St Apt ***


有人可以告诉我我在做什么错吗?谢谢!!

public String mask(String address) {
        String[] splitAddress = address.split(" ");
        StringBuilder stringBuilder = new StringBuilder();
        String maskedAddress = "";

    String streetNum = splitAddress[0];
    stringBuilder.append(streetNum + " ");



    for (int i = 1; i < splitAddress.length; i++) {
                String splitFirstTwoCharacters = splitAddress[i].substring(0, 2);
                String remainingCharactersOfAddress = splitAddress[i].substring(2);
                String maskAddress = remainingCharactersOfAddress.replaceAll(".", "*");
                maskedAddress = stringBuilder.append(splitFirstTwoCharacters).append(maskAddress + " ").toString().trim();
    }
 return maskedAddress;
}

最佳答案

当您执行splitAddress[i].substring(0, 2)时,splitAddress部分之一是B,它没有endIndex2。因此,它是超出范围的。

关于java - 屏蔽-带星号的地址(星号)-如果数组的长度为1?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/59293645/

10-10 06:42