📅  最后修改于: 2020-09-26 03:01:05             🧑  作者: Mango
java的字符串 concat()方法联合机在这一字符串的末尾指定的字符串 。它返回组合字符串。就像附加另一个字符串。
"public String concat(String str) {
int otherLen = str.length();
if (otherLen == 0) {
return this;
}
int len = value.length;
char buf[] = Arrays.copyOf(value, len + otherLen);
str.getChars(buf, len);
return new String(buf, true);
}
字符串 concat()方法的签名如下:
"public String concat(String anotherString)
anotherString:另一个字符串,即要在此字符串的末尾组合的字符串。
组合后的字符串
"public class ConcatExample{
public static void main(String args[]){
String s1="java string";
s1.concat("is immutable");
System.out.println(s1);
s1=s1.concat(" is immutable so assign it explicitly");
System.out.println(s1);
}}
"java string
java string is immutable so assign it explicitly
让我们看一个连接多个字符串对象的示例。
"public class ConcatExample2 {
public static void main(String[] args) {
String str1 = "Hello";
String str2 = "Javatpoint";
String str3 = "Reader";
// Concatenating one string
String str4 = str1.concat(str2);
System.out.println(str4);
// Concatenating multiple strings
String str5 = str1.concat(str2).concat(str3);
System.out.println(str5);
}
}
输出:
"HelloJavatpoint
HelloJavatpointReader
让我们看一个将空格和特殊字符连接到字符串对象的示例。
"public class ConcatExample3 {
public static void main(String[] args) {
String str1 = "Hello";
String str2 = "Javatpoint";
String str3 = "Reader";
// Concatenating Space among strings
String str4 = str1.concat(" ").concat(str2).concat(" ").concat(str3);
System.out.println(str4);
// Concatenating Special Chars
String str5 = str1.concat("!!!");
System.out.println(str5);
String str6 = str1.concat("@").concat(str2);
System.out.println(str6);
}
}
输出:
"Hello Javatpoint Reader
Hello!!!
Hello@Javatpoint