English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
La méthode concat() de chaîne de caractères de Java connecte (connecte) deux chaînes de caractères et la retourne.
La syntaxe de la méthode concat() de chaîne de caractères est :
str.concat(String str1)
str1 - Les chaînes de caractères à concaténer
Retourne une chaîne de caractères qui est str et str1La chaîne de caractères concaténée (chaîne de caractères de paramètre)
class Main {}} public static void main(String[] args) { String str1 = "Learn"; String str2 = "Java"; //concat str1and str2 System.out.println(str1.concat(str2)); // "Learn Java" // concat str2and str11 System.out.println(str2.concat(str1)); // "JavaLearn " } }
En Java, vous pouvez également utiliser + Les opérateurs pour joindre deux chaînes de caractères. Par exemple,
class Main {}} public static void main(String[] args) { String str1 = "Learn"; String str2 = "Java"; //concat str1and str2 System.out.println(str1 + str2); // "Learn Java" //concat str2and str11 System.out.println(str2 + str1); // "JavaLearn " } }
concat() | +operator |
---|---|
Assume str1is null, str2is "Java". Then, str1.concat(str2) throwsNullPointerException. | Assumestr1is null,str2is "Java". Then, str1 + str2Give"nullJava". |
You can only pass String to the concat() method. | If one of the operands is a string and the other is not a string value. Before the connection, non-string values will be internally converted to strings. For example, "Java" + 5Give "Java5". |