阅读量:125
在Java中有几种方法可以拼接多个字符串:
- 使用"+"符号:
String str1 = "Hello";
String str2 = "World";
String result = str1 + " " + str2;
System.out.println(result); // 输出:Hello World
- 使用String.concat()方法:
String str1 = "Hello";
String str2 = "World";
String result = str1.concat(" ").concat(str2);
System.out.println(result); // 输出:Hello World
- 使用StringBuffer或StringBuilder类:
String str1 = "Hello";
String str2 = "World";
StringBuilder sb = new StringBuilder();
sb.append(str1).append(" ").append(str2);
String result = sb.toString();
System.out.println(result); // 输出:Hello World
- 使用String.join()方法:
String str1 = "Hello";
String str2 = "World";
String result = String.join(" ", str1, str2);
System.out.println(result); // 输出:Hello World