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