Reputation: 151
I have a string ABC2022050400000000 and last digits should be incremented .Trying with the below logic and the string is incrementing line below .Something is wrong with the syntax .Could anyone help me please ABC2022050400000000 ABC2022050400000001
It should be like this: ABC2022050400000001 ABC2022050400000002 to ABC2022050400002110
for(int i=0;i<paddedID.length();i++) {
String newTitleSearch;
int len = paddedID.length();
String allButLast = paddedID.substring(0, len - 1);
System.out.println(allButLast);
int val=1;
newTitleSearch = allButLast + new Character((char) (paddedID.charAt(len - 1) +val++));
System.out.println(newTitleSearch);
}
Upvotes: 0
Views: 51
Reputation: 16498
You could split the input between letters and digit using either substring method if the prefix ABC
has a fixed length or using regex if it is dinamic. Then parse the digit part to long and increment it and concatenate back:
public static void main(String[] args) {
String input = "ABC2022050400000000";
String[] split = input.split("(?<=\\D)(?=\\d)");
String prefix = split[0];
long initial = Long.parseLong(split[1]);
for (long i = initial; i < initial + 50; i++){
System.out.println(prefix+i);
}
}
Upvotes: 1