java concat方法怎么使用
在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
方法都会将两个字符串连接起来,并返回一个新的字符串对象。原始的字符串对象保持不变。
阅读剩余
THE END