如何连接字符而不是相加数值。

huangapple go评论77阅读模式
英文:

How to concatenate chars rather than add the numeric values

问题

如何将字符数字与字符数字连接起来?
我的代码是:

String room = "901";
// 我想要一个新的字符串"01",所以我这样做

String roomNum = room.charAt(1) + room.charAt(2); // 这是错误的

String roomNum = String.valueOf(room.charAt(1)) + room.charAt(2); // 这也是错误的

String roomNum = room.substring(1, 3); // 这是正确的做法

当我使用String.valueOf()时,它会给我一个ASCII值。

英文:

How to connect char number with char number?
My code is:

String room = "901";
// I want to a new String "01" so i do this

String roomNum = room.charAt(1) + room.charAt(2); // it's error

String roomNum = h.charAt(0).concat(h.charAt(1)); // error too

When I use String.valueOf() it gives me an ASCII.

答案1

得分: 4

String room = "901";
//我想要一个新的字符串"01":

使用
    
    room.substring(1);

[`String#substring(int)`](https://docs.oracle.com/en/java/javase/11/docs/api/java.base/java/lang/String.html#substring(int)) 返回以从`int`开始并以最后一个字符结束的子字符串。

有关更多的`substring`重载请查看[String API](https://docs.oracle.com/en/java/javase/11/docs/api/java.base/java/lang/String.html)。

---

如果您想要*连接两个字符*您可以这样做

```java
String room = "901";

char a = room.charAt(1);
char b = room.charAt(2);

String result = String.valueOf(a).concat(String.valueOf(b));

否则,String roomNum = 'a' + 'b'; 将无法编译,因为在Java中,chars、shorts或bytes的相加结果是int

请注意,char 表示一个Unicode字符(在Java中,每个字符占16位)。因此,每个char值在内部都以数值进行编码,该数值以十六进制、十进制或其他基数系统数字的形式存储。这是char值上的算术操作会提升为int的主要原因。


<details>
<summary>英文:</summary>

```java
String room = &quot;901&quot;;
//I want to a new String &quot;01&quot;:

Use:

room.subString(1);

String#subString(int) returns substring starting with int and ending with the last character.

For more subString overloads, have a look at String API.


If you want to concatenate two chars, you can do:

String room = &quot;901&quot;;

char a = room.charAt(1);
char b = room.charAt(2);

String result = String.valueOf(a).concat(String.valueOf(b));

Otherwise, String roomNum = &#39;a&#39;+&#39;b&#39;; will not compile, as the result of adding Java chars, shorts, or bytes is an int.

Beware, that char represents an Unicode Character (in Java, 16-bits each). So, each char value, under the hood, is encoded by numeric value, which is stored as a hexadecimal, decimal or other radix-system number. This is the main reason, why arithmetic operation on char value promotes to int.

答案2

得分: 2

使用substring将解决问题(正如@Giorgi Tsiklauri的答案中所述),但这并没有指向实际隐藏的问题,即:

为什么字符之间的连接不同于大小为1的两个字符串的连接?

这是因为在这种上下文中,+符号不起作用作为连接。

当您在字符上应用+运算符时,操作之前会进行到int的转换。这个操作被称为二进制数值提升。来自JLS中的第二点:

> 扩大原始转换(§5.1.2)适用于根据以下规则指定的方式转换一个或两个操作数:
>
> 如果任一操作数的类型为double,则将另一个操作数转换为double。
>
> 同样,如果任一操作数的类型为float,则将另一个操作数转换为float。
>
> 同样,如果任一操作数的类型为long,则将另一个操作数转换为long。
>
> 否则,两个操作数都将转换为int类型。

因此,如果您想要对0字符的string值和1字符的string值进行求和,您需要显式地将它们转换为字符串,如下所示:

String room = "901";

String roomNumber = String.valueof(room.charAt(1)) + String.valueof(room.charAt(2));

如果您这样做,+运算符将被视为字符串之间的连接,而不是int之间的求和。

英文:

While using substring will solve the problem (as in the answer of @Giorgi Tsiklauri), this doesn't point to the real hidden question posted that is:

Why a concatenation between chars is not the same of a concatenation of two strings of size 1 ?

That happens because the + symbol doesn't work as a concatenation in this context.

When you apply the + operator on chars a conversion to int is done before the operation. This operation is called Binary Numeric Promotion. From the second point in the JLS:

> Widening primitive conversion (§5.1.2) is applied to convert either or both operands as specified by the following rules:
>
> If either operand is of type double, the other is converted to double.
>
> Otherwise, if either operand is of type float, the other is converted to float.
>
> Otherwise, if either operand is of type long, the other is converted to long.
>
> Otherwise, both operands are converted to type int.

So if you want to sum the string value of the 0 char and the string value of the 1 char you explicitly need to convert them in string as follow:

String room = &quot;901&quot;;

String roomNumber = String.valueof(room.charAt(1)) + String.valueof(room.charAt(2));

If you do that the + operator is considered a concatenation between strings and not as a sum between int.

huangapple
  • 本文由 发表于 2020年10月1日 23:32:14
  • 转载请务必保留本文链接:https://go.coder-hub.com/64158593.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定