在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
方法都会将两个字符串连接起来,并返回一个新的字符串对象。原始的字符串对象保持不变。
亿速云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>
推荐阅读:JavaScript Array concat()方法怎么使用