Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java - Append quotes to strings in an array and join strings in an array

I would like to append double quotes to strings in an array and then later join them as a single string (retaining the quotes). Is there any String library which does this? I have tried Apache commons StringUtils.join and the Joiner class in Google guava but couldn't find anything that appends double quotes.

My input would be an array as mentioned below:

String [] listOfStrings = {"day", "campaign", "imps", "conversions"};

Required output should be as mentioned below:

String output = "\"day\", \"campaign\", \"imps\", \"conversions\"";

I know I can loop through the array and append quotes. But I would like a more cleaner solution if there is one.

like image 865
Anand Avatar asked Aug 14 '13 09:08

Anand


People also ask

How do I combine string arrays into strings?

Create an empty String Buffer object. Traverse through the elements of the String array using loop. In the loop, append each element of the array to the StringBuffer object using the append() method. Finally convert the StringBuffer object to string using the toString() method.

How do you join elements in an array Java?

Java – Join Elements of String Array with Delimiter To join elements of given string array strArray with a delimiter string delimiter , use String. join() method. Call String. join() method and pass the delimiter string delimiter followed by the string array strArray .


2 Answers

With Java 8+

Java 8 has Collectors.joining() and its overloads. It also has String.join.

Using a Stream and a Collector

The naive but effective way

String wrapWithQuotesAndJoin(List<String> strings) {   return strings.stream()     .map(s -> "\"" + s + "\"")     .collect(Collectors.joining(", ")); } 

Shortest and probably better performing (somewhat hackish, though)

String wrapWithQuotesAndJoin(List<String> strings) {   return strings.stream()     .collect(Collectors.joining("\", \"", "\"", "\"")); } 

Using String.join

Very hackish. Don't use. (but it must be mentioned)

String wrapWithQuotesAndJoin(List<String> strings) {   return strings.isEmpty() ? "" : "\"" + String.join("\", \"", strings) + "\"" } 

With older versions of Java

Do yourself a favor and use a library. Guava comes immediately to mind.

Using Guava

private static final Function<String,String> addQuotes = new Function<String,String>() {   @Override public String apply(String s) {     return new StringBuilder(s.length()+2).append('"').append(s).append('"').toString();   } }; String wrapWithQuotesAndJoin(List<String> strings) {          return Joiner.on(", ").join(Iterables.transform(listOfStrings, addQuotes)); } 

No libraries

String wrapWithQuotesAndJoin(List<String> strings) {   if (listOfStrings.isEmpty()) {     return "";   }   StringBuilder sb = new StringBuilder();   Iterator<String> it = listOfStrings.iterator();   sb.append('"').append(it.next()).append('"'); // Not empty   while (it.hasNext()) {     sb.append(", \"").append(it.next()).append('"');   }   result = sb.toString(); } 

Notes:

  • All the solutions assume that strings is a List<String> rather than a String[]. You can convert a String[] into a List<String> using Arrays.asList(strings). You can get a Stream<String> directly from a String[] using Arrays.stream(strings).
  • The Java 8+ snippets use the + concatenation because at this point + is usually better performing than StringBuilder.
  • The older-version snippets use StringBuilder rather than + because it's usually faster on the older versions.
like image 132
Olivier Grégoire Avatar answered Sep 30 '22 04:09

Olivier Grégoire


String output = "\"" + StringUtils.join(listOfStrings , "\",\"") + "\"";
like image 29
Sifeng Avatar answered Sep 30 '22 02:09

Sifeng