简体   繁体   中英

Concatenate or Merge strings (JAVA)

How can I Concatenate or Merge a string after another string in java? for example I have this first string:

String1="12345"

and this is second string:

String2="00000"

How to concatenate the second string after first string? Output is:

String3="1234500000"

You can use the + operator to concatenate them. Give your variables small first letters: string1

Plus operator for printing:

System.out.println(string1 + string2);

Or store in a third String (string3)

string3 = string1 + string2;

There is the string1.concat(string2) function, but if there is a Null value it will NPE . Furthermore, the concat() method only accepts String values while the + operator will silently convert the argument to a String (using the toString() method for objects) See here for more on Concat and +

You can also do the following:

String string1 = "12345";
String string2 = "00000";

string1 += string2;

System.out.println(string1);

By doing it this way, you can eliminate the string3 variable.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM