共計 494 個字符,預計需要花費 2 分鐘才能閱讀完成。
在 Java 中,可以使用 concat
方法將兩個字符串連接起來。concat
方法是 String
類的一個方法,用于將當前字符串與另一個指定的字符串連接起來,并返回一個新的字符串對象。
使用 concat
方法有兩種方式:
- 使用對象調用方法:將要連接的字符串作為參數傳遞給現有字符串的
concat
方法。
String str1 = "Hello";
String str2 = "World";
String result = str1.concat(str2);
System.out.println(result); // 輸出:HelloWorld
- 使用靜態方法:直接使用
String
類的靜態方法concat
,將要連接的字符串作為參數傳遞給該方法。
String str1 = "Hello";
String str2 = "World";
String result = String.concat(str1, str2);
System.out.println(result); // 輸出:HelloWorld
無論使用哪種方式,concat
方法都會將兩個字符串連接起來,并返回一個新的字符串對象。原始的字符串對象保持不變。
丸趣 TV 網 – 提供最優質的資源集合!
正文完