16

I have a below String as -

String current_version = "v2";

And here version can be either of these string as well - v3 or v4 or v10 or v100 or v500 or v1000

Now I am looking to increment the current_version from v2 to v3. Meaning I always need to increment the current_version by 1.

If current_version is v5, then I will increment it to v6.

How can I do this with a String?

NOTE:-

I am getting current_version string value from some method which will always return me like this v2, v3, v4 only..

8 Answers8

13

If you want to parse the number after the v as an int and increment it by 1, just do so by substringing, adding 1 to the number and concatenating the resulting incremented number back into a string with "v" prepended:

    String version = "v1";
    String newVersion = "v" + (Integer.parseInt(version.substring(1,version.length()))+1);
    System.out.println(newVersion);

Alternatively, you could keep track of the current version in an int, increment that and construct your new String by appending that int to a "v", without having to call substring().

Martin Dinov
  • 8,757
  • 3
  • 29
  • 41
11

I will suggest you to store the version in an int. so the codes will be following

int versionNumber = 1;

whenever you want to increment just increment it using

versionNumber++;

and to print or store as a complete version do following

String version = "v" + versionNumber;
stinepike
  • 54,068
  • 14
  • 92
  • 112
2

try

int version = Integer.parseInt(current_version.replace("v", ""));

then you can increment it and add a get "v" + current_version

exception1
  • 1,239
  • 8
  • 17
1

If your format ever changes, this allows for a bit more flexibility:

String version = "v1234";
System.out.println("v" + (Integer.parseInt(version.replaceAll("[^0-9]", "")) + 1));

Use a regex to drop all non-digits, add one to the result.

Zavior
  • 6,412
  • 2
  • 29
  • 38
1
    String oldVersion = "v100";
    String[] splitString = oldVersion.split("v");
    int newVersion = Integer.valueOf(splitString[1])+1;
    String completeNewVersion = splitString[0] + newVersion;
    System.out.print(completeNewVersion);
ValerioMC
  • 2,926
  • 13
  • 24
0

Why you don't store the version as an int and add the char 'v' when displaying the version? If you wish to do it with a String, you can make a substring of the initial one, parse it to int and after that add to'v' the result.

 currentVersion = "v" + (Integer.parseInt(currentVersion .substring(1)) + 1);
Slimu
  • 2,301
  • 1
  • 22
  • 28
0

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);
nhaggen
  • 301
  • 2
  • 8
-1

If you want another solution you can use this :

uint8_t versionNumber = 1;
char version[40];
sprintf(version,"v %d",versionNumber);

And you increment your variable "versionNumber" whenever you want !

B. Delbart
  • 41
  • 5