Java 中的字符串连接
在 Java 中,可以用 concat() 方法或使用“+”(连接操作符)连接两个字符串。
concat() 方法
concat() 方法将一个字符串追加到另一个字符串末尾。此方法返回一个字符串,其值为传入该方法的字符串追加到调用此方法的字符串末尾后的结果。
示例
public class ConncatSample {
public static void main(String []args) {
String s1 = "Hello";
String s2 = "world";
String res = s1.concat(s2);
System.out.print("Concatenation result:: ");
System.out.println(res);
}
}输出
Concatenation result:: Helloworld
“+”操作符
与 concat 方法一样,“+”操作符对给定的操作数也执行连接操作。
示例
public class ConncatSample {
public static void main(String []args) {
String s1 = "Hello";
String s2 = "world";
String res = s1+s2;
System.out.print("Concatenation result:: ");
System.out.println(res);
}
}输出
Concatenation result:: Helloworld
广告
数据结构
网络
RDBMS
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程
C++
C#
MongoDB
MySQL
JavaScript
PHP