-4

I have the following Java code :

String Name = "";
String n1 = "";
String n2 = "";
String n3 = "";
String n4 = "";
n1= getGName();
n2= getSo();
n3=getSNe();
n4=getMName();

How to concatenate the strings such that name=n1_n2n3_n4

Not a bug
  • 4,286
  • 2
  • 40
  • 80
user3239377
  • 3
  • 2
  • 4
  • If you only have these 4 Strings, use `+`. Worth looking at [`StringBuilder`](http://docs.oracle.com/javase/7/docs/api/java/lang/StringBuilder.html). – Maroun Apr 09 '14 at 06:24
  • 1
    Why are you initializing the variables and then ignoring their values? Why not `String n1 = getGName(); String n2 = getSo();` etc? – Jon Skeet Apr 09 '14 at 06:24
  • 4
    This question appears to be off-topic because OP **doesn't** show minimal understanding of the problem being solved. – Maroun Apr 09 '14 at 06:26
  • String concatenation is a feature that has been planned for Java 11. Keep watching. – devnull Apr 09 '14 at 06:26

5 Answers5

2

Something like

String name = String.format("%s_%s%s_%s",n1,n2,n3,n4);

Using the String.format function

Using the String.format style (similar to C's 'printf' syntax) allows you to see the structure of your final string more clearly even when variable names are long. Overall it makes code easier to read then using the + operator, because you're separating your formatting text from the list of values you want in that format.

Community
  • 1
  • 1
Chad Okere
  • 4,570
  • 1
  • 21
  • 19
0
String name = n1 + "_" + n2 + n3 + "_" + n4;
Smutje
  • 17,733
  • 4
  • 24
  • 41
  • You might want to mention [`StringBuilder`](http://docs.oracle.com/javase/7/docs/api/java/lang/StringBuilder.html) as well. – Maroun Apr 09 '14 at 06:24
  • I considered this first, but recently I started listening to my IDE (IntelliJ) which repeatedly tells me *not* to use `StringBuilder` if no control structures are included (for performance reasons, I think). – Smutje Apr 09 '14 at 06:26
  • @Smutje StringBuilder is faster in terms of performance – Baby Apr 09 '14 at 06:27
  • Without evidence, this statement is not useful, but I refer now to http://stackoverflow.com/questions/1532461/stringbuilder-vs-string-concatenation-in-tostring-in-java where it states that the Java spec says "To increase the performance of repeated string concatenation, a Java compiler _may_ use the StringBuffer class or a similar technique to reduce the number of intermediate String objects that are created by evaluation of an expression." and therefore the concatenation of `String` is preferable because of its less boilerplate code. – Smutje Apr 09 '14 at 06:31
  • 1
    @Smutje If you want evidence, you may try `String str="";for(int i=0;i<1000;i++)str+="bla3";` and `String str="";StringBuilder sb = new StringBuilder(1000);for(int i=0;i<1000;i++){sb.append("bla3");}str=sb.toString();` see the difference – Baby Apr 09 '14 at 06:37
  • Sorry, but you have mistaken the problem - if one has control structures included, `StringBuilder` is without question preferably (because there does not exist something like a mutable `String`) - but the interesting part is when creating a `String` out of a static list of `String` *without* `if` or loops included. But this is already included in my first comment answer to @MarounMaroun. – Smutje Apr 09 '14 at 06:40
  • @Smutje Oh my bad, didn't read your previous comment -_- – Baby Apr 09 '14 at 06:48
0
n1 + "_" + n2 + n3 + "_" + n4

use it

Deepu--Java
  • 3,742
  • 3
  • 19
  • 30
0

Simply

String name = n1+"_"+n2+n2+"_";
tutak
  • 1,120
  • 1
  • 15
  • 28
0

u may try StringBuilder:

StringBuilder sb = new StringBuilder(getGName());
sb.append("_");
sb.append(getSo());
sb.append(getSNe());
sb.append("_");
sb.append(getMName());

String name = sb.toString();