This method is a bit more complex, but it is able to handle any kind of version-string. The currentVersion might already have a number defined, or it might be implicitly 1, major and minor version numbers can be present, only the last one is incremented.
The string is searched from the end until the first non-digit character is found. The versionNumber is computed as an integer and finally increased.
Then the newVersion is created. If the incremented versionNumber is 1, it means that the original String did not end in a number. In this case the versioning is started with _v2
String currentVersion = "test_v123.432";
int versionNumber = 0;
int power = 1;
int index = currentVersion.length()-1;
while (index > 0) { // get the last chars as long as they are digits
char digit = currentVersion.charAt(index);
if ((digit >= '0') && (digit <= '9')) {
versionNumber += (digit -'0') * power;
power = power * 10;
index --;
} else {
break;
}
}
versionNumber ++;
String newVersion = currentVersion.substring(0, index+1) + ((versionNumber == 1)? "_v2": versionNumber);