2014-10-10 92 views
0

我想在字符串中自動遞增整數(是產品代碼) 例如:ABC00001 - > ABC00002; ABC00009 - > ABC00010 ....和ABC99999 - > ABC000001 ... 它只是增加整數。我不知道如何解決它,因爲它的編號爲「0000」。 我搜索一個功能:混合字符串中增加的整數包括:字符串+整數

public static void main(String[] args) { 

    Pattern digitPattern = Pattern.compile("(\\d)"); // EDIT: Increment each digit. 

    Matcher matcher = digitPattern.matcher("test001check2"); 
    StringBuffer result = new StringBuffer(); 
    while (matcher.find()) 
    { 
     matcher.appendReplacement(result, String.valueOf(Integer.parseInt(matcher.group(1)) + 1)); 
    } 
    matcher.appendTail(result); 
    System.out.println(result.toString()); 

    } 

但它摺痕每個數字。我如何解決它?

回答

0
public static void main(String[] args) { 
    // use \d+ to match the whole number 
    Pattern digitPattern = Pattern.compile("(\\d+)"); 

    Matcher matcher = digitPattern.matcher("test001check2"); 
    StringBuffer result = new StringBuffer(); 
    while (matcher.find()) 
    { 
     String match = matcher.group(1); 
     int numDigits = match.length(); // get number of digits in the string 
     int newValue = Integer.parseInt(matcher.group(1)) + 1; // get the raw value of the string, add one 
     // pad the new value with the right number of zeros, so 001 will become 002 and not just 2 
     String newValueStr = String.format("%0" + numDigits + "d", newValue); 
     matcher.appendReplacement(result, newValueStr); 
    } 
    matcher.appendTail(result); 
    System.out.println(result.toString()); 

    } 

輸出在我的機器上:test002check3

0

如果我理解你的問題,一種可能的方法是從String中取出前3個字符,然後將其他所有內容提取到int中。然後,你可以設置輸出格式(和其解壓縮到一個方法)喜歡的東西,

private static String increment(String str) { 
    String base = str.substring(0, 3); 
    int val = Integer.parseInt(str.substring(4)); 
    return String.format("%s%06d", base, val + 1); 
} 

然後對其進行測試

public static void main(String[] args) { 
    String str = "ABC000001"; 
    System.out.println(str); 
    System.out.println(increment(str)); 
} 

輸出是

ABC000001 
ABC000002 
0

好的,對不起,我的問題並不清楚。我只是想增加混合字符串中的最後一個整數。 ex:test001check2 - > test001check3; test001check9-> test001check00; test001check09 - > test001check10; test001check99 - > test001check000 .... 現在我發現了一個具有上述要求的函數。

public final char MIN_DIGIT = '0'; 
public final char MAX_DIGIT = '9'; 

public String incrementedCode(String original) { 
    if(original != null && !"".equals(original.trim())){ 
     StringBuilder buf = new StringBuilder(original); 
     int i = buf.length() -1; 
     while(i >= 0) { 
      char c = buf.charAt(i); 
      if(c >= MIN_DIGIT && c<= MAX_DIGIT){ 
       c++; 
       if(c > MAX_DIGIT) { // overflow, carry one 
        if(buf.charAt(i - 1) > MAX_DIGIT){ 
         buf.setCharAt(i, MIN_DIGIT); 
         buf.insert(i, "0"); 
         return buf.toString(); 
        } 
        buf.setCharAt(i, MIN_DIGIT); 
        i--; 
        continue; 
       } 
       buf.setCharAt(i, c); 
       return buf.toString(); 
      } 
      i--; 
     } 
     // overflow at the first "digit", need to add one more digit 
     buf.insert(0, MIN_DIGIT); 
     return buf.toString(); 
    }else{ 
     return ""; 
    } 
}