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
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
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.
String name = n1 + "_" + n2 + n3 + "_" + n4;
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();