如何在java中连接int值?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2674707/
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
How to concatenate int values in java?
提问by Shamli
I have the following values:
我有以下价值观:
int a=1;
int b=0;
int c=2;
int d=2;
int e=1;
How do i concatenate these values so that i end up with a String that is 10221
;
please note that multiplying a
by 10000, b
by 1000.....and e
by 1 will not working since b=0
and therefore i will lose it when i add the values up.
我如何连接这些值,以便我最终得到一个字符串10221
;请注意,乘以a
10000、乘以b
1000.....再e
乘以 1 将不起作用b=0
,因此当我将这些值相加时我会丢失它。
回答by T .
Actually,
实际上,
int result = a * 10000 + b * 1000 + c * 100 + d * 10 + e;
String s = Integer.toString(result);
willwork.
会工作。
Note: this will only work when a
is greater than 0 and all of b
, c
, d
and e
are in [0, 9]. For example, if b
is 15, Michael's method will get you the result you probably want.
注意:这仅在a
大于 0 且所有b
、c
、d
和e
都在 [0, 9] 中时才有效。例如,如果b
是 15,Michael 的方法将为您提供您可能想要的结果。
回答by Daniel A. White
If you multiply b
by 1000, you will not lose any of the values. See below for the math.
如果乘以b
1000,则不会丢失任何值。请参阅下面的数学。
10000
0
200
20
1
=====
10221
回答by Bj?rn
StringBuffer sb = new StringBuffer();
sb.append(a).append(b).append(c)...
Keeping the values as an int is preferred thou, as the other answers show you.
正如其他答案向您展示的那样,将值保留为 int 是首选。
回答by Grundlefleck
I would suggest converting them to Strings.
我建议将它们转换为字符串。
StringBuilder concatenated = new StringBuilder();
concatenated.append(a);
concatenated.append(b);
/// etc...
concatenated.append(e);
Then converting back to an Integer:
然后转换回整数:
Integer.valueOf(concatenated.toString());
回答by Shervin Asgari
Use StringBuilder
使用 StringBuilder
StringBuilder sb = new StringBuilder(String.valueOf(a));
sb.append(String.valueOf(b));
sb.append(String.valueOf(c));
sb.append(String.valueOf(d));
sb.append(String.valueOf(e));
System.out.print(sb.toString());
回答by Jon Skeet
Others have pointed out that multiplying b
by 1000 shouldn't cause a problem - but if awere zero, you'd end up losing it. (You'd get a 4 digit string instead of 5.)
其他人指出乘以b
1000 不应该引起问题 - 但如果a为零,你最终会失去它。(你会得到一个 4 位数的字符串,而不是 5。)
Here's an alternative (general purpose) approach - which assumes that all the values are in the range 0-9. (You should quite possibly put in some code to throw an exception if that turns out not to be true, but I've left it out here for simplicity.)
这是另一种(通用)方法 - 假设所有值都在 0-9 范围内。(如果结果不是真的,你很可能应该输入一些代码来抛出异常,但为了简单起见,我在这里把它省略了。)
public static String concatenateDigits(int... digits)
{
char[] chars = new char[digits.length];
for (int i = 0; i < digits.length; i++)
{
chars[i] = (char)(digits[i] + '0');
}
return new String(chars);
}
In this case you'd call it with:
在这种情况下,您可以使用以下命令调用它:
String result = concatenateDigits(a, b, c, d, e);
回答by Michael Borgwardt
The easiest (but somewhat dirty) way:
最简单(但有点脏)的方法:
String result = "" + a + b + c + d + e
Edit:I don't recommend this and agree with Jon's comment. Adding those extra empty strings is probably the best compromise between shortness and clarity.
编辑:我不推荐这个并且同意 Jon 的评论。添加那些额外的空字符串可能是简短和清晰之间的最佳折衷。
回答by polygenelubricants
Michael Borgwardt's solution is the best for 5 digits, but if you have variable number of digits, you can use something like this:
Michael Borgwardt 的解决方案最适合 5 位数,但如果您的位数可变,则可以使用如下方法:
public static String concatenateDigits(int... digits) {
StringBuilder sb = new StringBuilder(digits.length);
for (int digit : digits) {
sb.append(digit);
}
return sb.toString();
}
回答by cHao
People were fretting over what happens when a == 0. Easy fix for that...have a digit before it. :)
人们担心当 a == 0 时会发生什么。很容易解决这个问题......在它之前有一个数字。:)
int sum = 100000 + a*10000 + b*1000 + c*100 + d*10 + e;
System.out.println(String.valueOf(sum).substring(1));
Biggest drawback: it creates two strings. If that's a big deal, String.format could help.
最大的缺点:它创建了两个字符串。如果这是一个大问题,String.format 可以提供帮助。
int sum = a*10000 + b*1000 + c*100 + d*10 + e;
System.out.println(String.format("%05d", sum));
回答by Ali
You can Use
您可以使用
String x = a+"" +b +""+ c+""+d+""+ e;
int result = Integer.parseInt(x);