Java 如何将字符串中的数字增加 1?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21938228/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 11:30:44  来源:igfitidea点击:

How to increment the number in a String by 1?

javastringincrement

提问by

I have a below String as -

我有一个下面的字符串 -

String current_version = "v2";

And here version can be either of these string as well - v3or v4or v10or v100or v500or v1000

并且这里的版本也可以是这些字符串中的任何一个 -v3v4v10v100v500v1000

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

现在我希望将 current_version 从v2增加到v3。这意味着我总是需要将 current_version 增加 1。

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

如果 current_version 是v5,那么我会将它增加到v6

How can I do this with a String?

我怎样才能用字符串做到这一点?

NOTE:-

笔记:-

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

current_version从某种方法中获取字符串值,它总是像这样返回我 v2、v3、v4。

采纳答案by Martin Dinov

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:

如果要将 v 后面的数字解析为 int 并将其递增 1,只需通过子串化、将 1 添加到数字并将结果递增的数字连接回带有“v”前缀的字符串来执行此操作:

    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 Stringby appending that int to a "v", without having to call substring().

另外,你可以跟踪的当前版本在int,增量是构建新的String通过追加INT一个“V”,而不必调用substring()

回答by Slimu

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.

为什么不将版本存储为 int 并在显示版本时添加字符“v”?如果你想用一个字符串来做,你可以创建一个初始字符串的子字符串,将它解析为 int ,然后将结果添加到 'v' 。

 currentVersion = "v" + (Integer.parseInt(currentVersion .substring(1)) + 1);

回答by exception1

try

尝试

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

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

然后你可以增加它并添加一个 get "v" + current_version

回答by stinepike

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

我建议您将版本存储在 int 中。所以代码将遵循

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;

回答by Zavior

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.

使用正则表达式删除所有非数字,在结果中添加一个。

回答by ValerioMC

    String oldVersion = "v100";
    String[] splitString = oldVersion.split("v");
    int newVersion = Integer.valueOf(splitString[1])+1;
    String completeNewVersion = splitString[0] + newVersion;
    System.out.print(completeNewVersion);

回答by B. Delbart

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 !

您可以随时增加变量“versionNumber”!