0

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"

2 Answers 2

1

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 +

Sign up to request clarification or add additional context in comments.

4 Comments

Is this operation stores string2 directly after string3?
What do you mean? string3 will be string1 followed by string2 or as in your example "1234500000"
???? That was downvoted already, I didn't touch the question at all! I went to the trouble of answering your question!
@MohammadFarahi No problem, my pleasure! I wouldn't downvote a question I've answered, and if I do downvote I add a comment saying why! Thanks for accepting.
1

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.