Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
434 views
in Technique[技术] by (71.8m points)

guava - 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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

With Java 8+

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

Using a Stream and a Collector

With a reusable function

Function<String,String> addQuotes = s -> """ + s + """;

String result = listOfStrings.stream()
  .map(addQuotes)
  .collect(Collectors.joining(", "));

Without any reusable function

String result = listOfStrings.stream()
  .map(s -> """ + s + """)
  .collect(Collectors.joining(", "));

Shortest (somewhat hackish, though)

String result = listOfStrings.stream()
  .collect(Collectors.joining("", "", """, """));

Using String.join

Very hackish. Don't use except in a method called wrapWithQuotesAndJoin.

String result = listOfString.isEmpty() ? "" : """ + String.join("", "", listOfStrings) + """;

With older versions of Java

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

Using Guava

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 result = Joiner.on(", ").join(Iterables.transform(listOfStrings, addQuotes));

No libraries

String result;
if (listOfStrings.isEmpty()) {
  result = "";
} else {
  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();
}

Note: all the solutions assume that listOfStrings is a List<String> rather than a String[]. You can convert a String[] into a List<String> using Arrays.asList(arrayOfStrings). You can get a Stream<String> directly from a String[] using Arrays.stream(arrayOfString).


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...