Everyone will experience the scenario where we need to join the strings in java .
These are the few ways we can do String concatenation in java 8.
Lets say we have 5 strings we want to make it 1 string with ',' separator .
String finalval = String.join(",", "First", "Second", "Third", "Forth");
output : First,Second,Third,Forth
In Case of array of String then,
String arry[] = { "Mango", "Apple", "Papaya" };
String finalval = String.join("-", arry);
output : Mango-Apple-Papaya
Lets say we want to append string to create IN params for SQL Query then,
import java.util.StringJoiner;
StringJoiner sj = new StringJoiner("','", "('", "')");
sj.setEmptyValue("It is empty");
System.out.println(sj.toString()); //prints 'It is empty' as nothing is there to join
sj.add("nike").add("Reebok").add("Puma").add("LV").add("Gucci");
System.out.println(sj.toString());
output:('nike','Reebok','Puma','LV','Gucci')
We can merge the two StringJoiners,
StringJoiner sj2 = new StringJoiner(",");
sj2.add("Fossil");
sj.merge(sj2);
System.out.println(sj.toString());
output:('nike','Reebok','Puma','LV','Gucci','Fossil')
Nice one
ReplyDeletebiggest web design companies
ReplyDeleteI truly appreciate your working guys, thumbs up!!
Their professionalism and enthusiasm were key to the app's success.
ReplyDeleteBay Area design firm
Open communication and timeliness were key to project success, They were a valuable partner.
ReplyDeletebest logo company
I am really appreciating very much by seeing your interesting posts.
ReplyDeletebest branding firms