简体   繁体   English

如何从 int 和 char 连接字符串?

[英]How can I concatanate a string from an int and a char?

For example I want to concatanate char a ='A' and int b = 5 into string = "A5" .例如,我想将char a ='A'int b = 5string = "A5"

String string = a + b; doesn't work.不起作用。

You may want to use StringBuilder , where you can append any type of primitives :您可能想要使用StringBuilder ,您可以在其中附加任何类型的原语:

char a ='A';
int b = 5;

StringBuilder sb = new StringBuilder();

sb.append(a);
sb.append(b);

String result = sb.toString();

The easiest way for me is to precede the String with an empty String.对我来说最简单的方法是在字符串前面加上一个空字符串。

 String str = "" + 'a'  + 10;

The conversion goes from left to right so you start out with a String.转换从左到右进行,因此您从字符串开始。

If you do it this way,如果你这样做,

String str = 'a' + 10 + ""; 

you will get a String value of "107" since the numeric addition is done before the conversion to a String.您将获得字符串值“107”,因为数字加法是在转换为字符串之前完成的。

One way to convert most primitive values to String is to utilize the overloaded method in the String class valueOf() :将大多数原始值转换为String的一种方法是利用StringvalueOf()的重载方法:

public static void main(String[] args) 
{
    char a = 'A';
    int b = 5;

    String str = String.valueOf(a) + b; //Can do either of these two lines, will work the same
    String str2 = a + String.valueOf(b);

    System.out.println(str);
    System.out.println(str2);
}

You only need to convert one of the values into String , because appending them afterward will automatically convert the other into a String .您只需要将其中一个值转换为String ,因为之后附加它们会自动将另一个值转换为String

This method will work on both char , and int in this scenario, but will also work on long , double , float and boolean as well.在这种情况下,此方法适用于charint ,但也适用于longdoublefloatboolean This is identical to calling Integer.toString(int i) or Character.toString(i) etc... but it is convenient to be able to use the same overloaded method for each case instead of requiring to call methods from different classes.这与调用Integer.toString(int i)Character.toString(i)等相同......但能够为每种情况使用相同的重载方法而不需要调用来自不同类的方法很方便。

To all the answers already given I provide one that explain why "it doesn´t work" as you expect.对于已经给出的所有答案,我提供了一个解释为什么“它不起作用”的原因。 A string is nothing but an immutable collection of char .一个字符串只不过是一个不可变的char集合。 A char itself is nothing but a (signed) integer and thus can implicitely converted to such. char本身只不过是一个(有符号的)整数,因此可以隐式转换为这样的整数。 Thus when you write char + int an integer-operation occurs, in your case A + 5 which results in 70 , because ASCII-code for A is 65.因此,当您编写char + int时会发生整数运算,在您的情况下A + 5结果为70 ,因为A ASCII 码是 65。

Last but not least String s = 70 surely does not compile, because a number can´t be converted to a string.最后但并非最不重要的String s = 70肯定不会编译,因为数字不能转换为字符串。

So you have to tell the compiler that you do not want an integer-operation, but a string-concatenation, which is by turning one of the operands into a string already:所以你必须告诉编译器你不想要一个整数运算,而是一个字符串连接,这是通过将一个操作数转换成一个字符串:

String s = 'A'.toString() + 5

or或者

String s = 'A' + 5.toString()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM