java 增加字符串值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5091355/
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
Increase string value
提问by hogni89
Java question here: If i have a string "a", how can I "add" value to the string, so I get a "b" and so on? like "a++"
这里的 Java 问题:如果我有一个字符串“a”,我如何向字符串“添加”值,所以我得到一个“b”等等?像“a++”
回答by Jigar Joshi
String str = "abcde";
System.out.println(getIncrementedString(str));
Output
输出
bcdef
定义
//this code will give next char in unicode sequence
//此代码将给出unicode序列中的下一个字符
public static String getIncrementedString(String str){
StringBuilder sb = new StringBuilder();
for(char c:str.toCharArray()){
sb.append(++c);
}
return sb.toString();
}
回答by Mark
If you use the char primitive data type you can accomplish this:
如果您使用 char 原始数据类型,您可以完成此操作:
char letter = 'a';
letter++;
System.out.println(letter);
prints out b
打印出 b
回答by Israel Dominguez
i made some changes to te paulo eberman code, to handle digits and characters, if valuable for someone i share this mod....
我对 te paulo eberman 代码进行了一些更改,以处理数字和字符,如果对我共享此 mod 的人有价值....
public final static char MIN_DIGIT = '0';
public final static char MAX_DIGIT = '9';
public final static char MIN_LETTER = 'A';
public final static char MAX_LETTER = 'Z';
public String incrementedAlpha(String original) {
StringBuilder buf = new StringBuilder(original);
//int index = buf.length() -1;
int i = buf.length() - 1;
//while(index >= 0) {
while (i >= 0) {
char c = buf.charAt(i);
c++;
// revisar si es numero
if ((c - 1) >= MIN_LETTER && (c - 1) <= MAX_LETTER) {
if (c > MAX_LETTER) { // overflow, carry one
buf.setCharAt(i, MIN_LETTER);
i--;
continue;
}
} else {
if (c > MAX_DIGIT) { // overflow, carry one
buf.setCharAt(i, MIN_DIGIT);
i--;
continue;
}
}
// revisar si es numero
buf.setCharAt(i, c);
return buf.toString();
}
// overflow at the first "digit", need to add one more digit
buf.insert(0, MIN_DIGIT);
return buf.toString();
}
i hope be usefull for someone.
我希望对某人有用。
回答by tvshajeer
use this code to increment char value by an integer
使用此代码将 char 值增加一个整数
int a='a';
System.out.println("int: "+a);
a=a+3;
char c=(char)a;
System.out.println("char :"+c);
回答by dogbane
- Convert the string to a char.
- Increment the char.
- Convert the char back to a string.
- 将字符串转换为字符。
- 增加字符。
- 将字符转换回字符串。
Example:
例子:
//convert a single letter string to char
String a = "a";
char tmp = a.charAt(0);
//increment char
tmp++;
//convert char to string
String b = String.valueOf(tmp);
System.out.println(b);
回答by Pa?lo Ebermann
Assuming you want something like aaab
=> aaac
and not => bbbc
, this would work:
假设您想要类似aaab
=>aaac
而不是 => 的东西bbbc
,这将起作用:
public String incremented(String original) {
StringBuilder buf = new StringBuilder(original);
int index = buf.length() -1;
while(index >= 0) {
char c = buf.charAt(i);
c++;
buf.setCharAt(i, c);
if(c == 0) { // overflow, carry one
i--;
continue;
}
return buf.toString();
}
// overflow at the first "digit", need to add one more digit
buf.insert(0, '');
return buf.toString();
}
This treats all characters (in fact char
values) the same, and fails (does strange stuff) for some unicode code-points outside the first plane (which occupy two char values in a String).
这将所有字符(实际上是char
值)视为相同,并且对于第一个平面(在字符串中占据两个字符值)之外的某些 unicode 代码点失败(做奇怪的事情)。
If you only want to use english lowercase lettersas digits, you can try this variant:
如果你只想使用英文小写字母作为数字,你可以试试这个变体:
public final static char MIN_DIGIT = 'a';
public final static char MAX_DIGIT = 'z';
public String incrementedAlpha(String original) {
StringBuilder buf = new StringBuilder(original);
int index = buf.length() -1;
while(index >= 0) {
char c = buf.charAt(i);
c++;
if(c > MAX_DIGIT) { // overflow, carry one
buf.setCharAt(i, MIN_DIGIT);
i--;
continue;
}
buf.setCharAt(i, c);
return buf.toString();
}
// overflow at the first "digit", need to add one more digit
buf.insert(0, MIN_DIGIT);
return buf.toString();
}
This does a
=> b
=> c
, y
=> z
=> aa
=> ab
.
这确实a
=> b
=> c
,y
=> z
=> aa
=> ab
。
if you want to do more calculation with the string, consider staying with StringBuilder (or StringBuffer for multithreaded access) instead of repeatedly copying between String and StringBuilder. Or use a class made to do this, like BigInteger.
如果您想对字符串进行更多计算,请考虑使用 StringBuilder(或用于多线程访问的 StringBuffer),而不是在 String 和 StringBuilder 之间重复复制。或者使用一个类来做到这一点,比如 BigInteger。