Java String concat() 示例
Java String concat() 方法将一个字符串连接到另一个字符串的末尾。此方法返回一个字符串,其中包含传递给该方法的字符串的值,附加到字符串的末尾。考虑下图:
插图:
Input: String 1 : abc
String 2 : def
String n-1 : ...
String n : xyz
Output: abcdef...xyz
句法:
public String concat(String anostr)
参数:要连接在另一个字符串末尾的字符串
返回:连接(组合)字符串
例子:
java
// Java program to Illustrate Working of concat() method
// with Strings
// By explicitly assigning result
// Main class
class GFG {
// Main driver method
public static void main(String args[])
{
// Custom input string 1
String s = "Hello ";
// Custom input string 2
s = s.concat("World");
// Explicitly assigning result by
// Combining(adding, concatenating strings)
// using concat() method
// Print and display combined string
System.out.println(s);
}
}
java
// Java program to Illustrate Working of concat() method
// in strings where we are sequentially
// adding multiple strings as we need
// Main class
public class GFG {
// Main driver method
public static void main(String args[])
{
// Custom string 1
String str1 = "Computer-";
// Custom string 2
String str2 = "-Science";
// Combining above strings by
// passing one string as an argument
String str3 = str1.concat(str2);
// Print and display temporary combined string
System.out.println(str3);
String str4 = "-Portal";
String str5 = str3.concat(str4);
System.out.println(str5);
}
}
输出
Hello World
示例 2:
Java
// Java program to Illustrate Working of concat() method
// in strings where we are sequentially
// adding multiple strings as we need
// Main class
public class GFG {
// Main driver method
public static void main(String args[])
{
// Custom string 1
String str1 = "Computer-";
// Custom string 2
String str2 = "-Science";
// Combining above strings by
// passing one string as an argument
String str3 = str1.concat(str2);
// Print and display temporary combined string
System.out.println(str3);
String str4 = "-Portal";
String str5 = str3.concat(str4);
System.out.println(str5);
}
}
输出
Computer--Science
Computer--Science-Portal
从代码中可以看出,我们可以做很多次,将绕过旧字符串的字符串与要被污染的新字符串作为参数连接起来,并将结果字符串存储在 String 数据类型中。