Naham Soft Naham Soft - 6 months ago 12
Java Question

How to convert arrayList to string separated by hyphen

I have String ArrayList has some elements but I need to convert it to string each element is separated from other by -
this is the arrayList

List<String> items = new ArrayList<String>();
items.add("a");
items.add("b");
items.add("c");
:
:


I need to get the string like this


"a-b-c-..."

Answer

You can use Joiner from google-guava:

List<String> items = new ArrayList<String>();
items.add("a");
items.add("b");
items.add("c");

String result = Joiner.on("-").join(items);

here is the link - https://github.com/google/guava/wiki/StringsExplained#joiner

Comments