如何在 Java 中连接两个字符串?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3753869/
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-14 04:26:30  来源:igfitidea点击:

How do I concatenate two strings in Java?

javastring-concatenation

提问by test

I am trying to concatenate strings in Java. Why isn't this working?

我正在尝试在 Java 中连接字符串。为什么这不起作用?

public class StackOverflowTest {  
    public static void main(String args[]) {
        int theNumber = 42;
        System.out.println("Your number is " . theNumber . "!");
    }
}

采纳答案by Peter Lang

You can concatenate Strings using the +operator:

您可以使用+运算符连接字符串:

System.out.println("Your number is " + theNumber + "!");

theNumberis implicitly converted to the String "42".

theNumber隐式转换为 String "42"

回答by Mateusz Dymczyk

"+" instead of "."

“+”代替“。”

回答by Bozho

The concatenation operator in java is +, not .

java中的连接运算符是+, not.

Read this(including all subsections) before you start. Try to stop thinking the php way ;)

在开始之前阅读本文(包括所有小节)。试着停止思考 php 的方式;)

To broaden your view on using strings in Java - the +operator for strings is actually transformed (by the compiler) into something similar to:

为了拓宽您在 Java 中使用字符串的观点 - 字符串的+运算符实际上(由编译器)转换为类似于以下内容的内容:

new StringBuilder().append("firstString").append("secondString").toString()

回答by Greg

Use +for string concatenation.

使用+字符串连接。

"Your number is " + theNumber + "!"

回答by Ed Manet

You must be a PHP programmer.

您必须是一名 PHP 程序员。

Use a +sign.

使用+标志。

System.out.println("Your number is " + theNumber + "!");

回答by Uncommented

In Java, the concatenation symbol is "+", not ".".

在 Java 中,连接符号是“+”,而不是“.”。

回答by bragboy

This should work

这应该工作

public class StackOverflowTest
{  
    public static void main(String args[])
    {
        int theNumber = 42;
        System.out.println("Your number is " + theNumber + "!");
    }
}

回答by JohnMetta

"+" not "."

“+”不是“。”

But be careful with String concatenation. Here's a link introducing some thoughts from IBM DeveloperWorks.

但要小心字符串连接。这是一个链接,介绍了IBM DeveloperWorks 的一些想法。

回答by DwB

There are two basic answers to this question:

这个问题有两个基本答案:

  1. [simple] Use the +operator (string concatenation). "your number is" + theNumber + "!"(as noted elsewhere)
  2. [less simple]: Use StringBuilder(or StringBuffer).
  1. [简单] 使用+运算符(字符串连接)。"your number is" + theNumber + "!"(如别处所述)
  2. [不太简单]:使用StringBuilder(或StringBuffer)。
StringBuilder value;
value.append("your number is");
value.append(theNumber);
value.append("!");

value.toString();

I recommend against stacking operations like this:

我建议不要像这样堆叠操作:

new StringBuilder().append("I").append("like to write").append("confusing code");

Edit: starting in java 5 the string concatenation operator is translated into StringBuildercalls by the compiler. Because of this, both methods above are equal.

编辑:从 java 5 开始,字符串连接运算符被StringBuilder编译器转换为调用。因此,上述两种方法是相同的。

Note: Spaceisavaluablecommodity,asthissentancedemonstrates.

注意:空间是一种有价值的商品,正如本句所示。

Caveat: Example 1 below generates multiple StringBuilderinstances and is less efficient than example 2 below

警告:下面的示例 1 生成多个StringBuilder实例并且效率低于下面的示例 2

Example 1

示例 1

String Blam = one + two;
Blam += three + four;
Blam += five + six;

Example 2

示例 2

String Blam = one + two + three + four + five + six;

回答by Learner

For exact concatenation operation of two string please use:

对于两个字符串的精确连接操作,请使用:

file_names = file_names.concat(file_names1);

In your case use +instead of .

在你的情况下使用+而不是.