Java 字符串连接是连接两个或多个字符串并返回一个新字符串的操作。此外,连接操作可用于将类型转换为字符串。您可以通过两种不同的方式在 Java 中连接字符串。
concat() 方法
Concat()方法将指定的字符串追加到给定字符串的末尾,然后返回,然后返回一个新形成的字符串。当然我们可以使用concat()方法连接三个或更多字符串。concat() 方法签名
public String concat(String str)
该方法将字符串 str 连接到当前字符串的末尾。例如:
String s1 = “Hello ”;
s1.concat("World")
返回一个新字符串“Hello World”。
Concat() 方法示例
让我们在初始化后连接两个字符串,然后连接更多字符串。还有一个更有趣的例子:我们将创建一个根据参数返回不同消息的方法。
public class ConcatTest {
public static void main(String[] args) {
//String concat with concat() method
String string1 = "I learn ";
String string2 = "concatenation in Java";
//here we use concat() method to join the two strings above
String result = string1.concat(string2);
System.out.println(result);
//concat() method to join 4 strings
String myWebSite = "Code".concat("Gym").concat(".cc").concat("/quest");
System.out.println(myWebSite); // here we've got "CodeGym.cc/quest"
System.out.println(myMessage(true));
System.out.println(myMessage(false));
}
private static String myMessage(boolean b) { //concat() method with selection statement
return "I have".concat(b ? " " : "n't ").concat("got it");
}
}
输出是:
我在 Java CodeGym.cc/quest 中学习连接 我知道了 我还没有
重要的是要知道concat()方法不会更改字符串,而是通过合并当前字符串和作为参数传递的字符串来创建一个新字符串。所以该方法返回一个新的String对象,这就是为什么你可以创建这么长的 String concat 链。
使用重载运算符“+”和“+=”连接
您可以像处理数字一样使用这两个运算符。它们的工作方式与concat()一样。让我们连接字符串“Code”和字符串“Gym”。
public class StringConcat {
public static void main(String[] args) {
String myString = "Code" + "Gym";
myString+= ".cc";
System.out.println(myString);
}
}
通常 Java 不允许对用户类进行重载操作,但这两个操作符作为重载工作。你可能认为 +运算符内部隐藏了concat(),但实际上,这里使用了另一种机制。如果我们查看 Java 程序字节码,我们会看到创建了StringBuilder并使用了它的append()方法。Java 编译器看到“+”运算符并意识到操作数是字符串,而不是原始类型。所以它像 concat 一样工作。
public class StringTest2 {
public static void main(String[] args) {
String hello = "hello";
String world = " world!";
String helloworld = (new StringBuilder().append(hello).append(world).toString());
System.out.println(helloworld);
//this is the same as:
String result = hello + world;
}
}
连接(0)或+?
如果您只需要合并一次字符串,请使用concat()方法。对于所有其他情况,最好使用+或Stringbuffer / StringBuilder运算符。此外,+运算符用于转换类型。如果其中一个操作数相等,例如,数字和第二个是字符串,那么在出口处我们将得到一个字符串。例子:
public class StringConcat {
public static void main(String[] args) {
int myInt = 5;
String myString = " days";
System.out.println(myInt + myString);
boolean b = (myInt + myString) instanceof String;
System.out.println(b);
}
}
我们使用instanceof来检查(myInt + myString)是否是一个字符串。这是该程序的输出:
5天真实
另请注意,如果其中一个字符串为空,则+运算符和concat() 的工作方式不同。如果在使用+或+=运算符时其中一个操作数只有一个——字符串,那么,正如我们已经说过的,类型转换就会发生。这同样适用于 null。我们将得到一个新字符串:
public class StringConcat {
public static void main(String[] args) {
String myString = "book ";
System.out.println(myString + null);
System.out.println((myString + null) instanceof String);
}
}
输出是:
预订无效
如果我们对相同的操作应用concat()方法,那么我们将得到一个NullPointerException。
public class StringConcat {
public static void main(String[] args) {
String myString = "book ";
System.out.println(myString.concat(null));
}
}
输出是:
StringConcat.main(StringConcat.java:6) 处的 java.base/java.lang.String.concat(String.java:1972) 线程“main”java.lang.NullPointerException 中的异常
GO TO FULL VERSION