字符串切割替换数字,使用ASCII值解决

@Test
public void test() {
    //审批人id集合,用逗号分隔,会签用-分隔,或签用@分隔,例如 1,2,3,4-5,6@7
    String testStr = "100,101,102-103,107@105";
    Map map = new HashMap() {
   {
        put("100", "99");
    }};
    int sIndex = 0;
    int eIndex;
    StringBuilder newTestStr = new StringBuilder();
    char[] chars = testStr.toCharArray();
    String temp;
    for (int i = 0; i < chars.length; i++) {
        byte b = (byte) chars[i];
        if (b > 57 || b < 48) {
            eIndex = i;
            temp = testStr.substring(sIndex, eIndex);
            newTestStr.append(map.getOrDefault(temp, temp));
            newTestStr.append(testStr.charAt(i));
            sIndex = i + 1;
        } else if (i == chars.length - 1) {
            temp = testStr.substring(sIndex, chars.length);
            newTestStr.append(map.getOrDefault(temp, temp));
        }
    }
    System.out.println(newTestStr.toString());
}

你可能感兴趣的:(知识温习)