Edie
Edie

Reputation: 65

Can I combine multiple concat?

I want to short my coding as much as I can. I tried to use "concat" but facing a problem to combine in multiple way. Did concat allow to combine two concat only (per concat)?

String concat1 = "Please Make A Choice :";
    String concat2 = "(1) For Calculate Area Of Circle";
    String concat3 = "(2) For Calculate Area Of Rectangle";
    String concat4 = "(3) For Calculate Area Of Triangle";
    String concat5 = concat1.concat(concat2,concat3,concat4);

Upvotes: 0

Views: 1711

Answers (2)

Jon Kiparsky
Jon Kiparsky

Reputation: 7743

concat takes one String argument, and appends it to the String instance it's called on. See the docs for the details.

You can use the + operator to do this

String concat5 = concat1+concat2+concat3+concat4;

But be aware that this is a relatively expensive process, and if you're doing a lot of concatenation the StringBuilder class is the thing to use instead.

Upvotes: 2

Madhawa Priyashantha
Madhawa Priyashantha

Reputation: 9872

you can use + operator to concat Strings but if you need to use keyword concat then this is how

concat5=  concat1.concat(concat2).concat(concat3).concat(concat4);

Upvotes: 1

Related Questions