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

前端 未结 7 715
后悔当初
后悔当初 2020-12-13 00:04

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?

相关标签:
7条回答
  • 2020-12-13 00:22
    String output = "\"" + StringUtils.join(listOfStrings , "\",\"") + "\"";
    
    0 讨论(0)
  • 2020-12-13 00:24

    You can create the code for this functionality yourself as well:

    String output = "";
    
    for (int i = 0; i < listOfStrings.length; i++)
    {
        listOfStrings[i] = "\"" + listOfStrings[i] + "\"";
        output += listOfStrings[i] + ", ";
    }
    
    0 讨论(0)
  • 2020-12-13 00:24
    public static void main(String[] args) {
        // TODO code application logic here
        String [] listOfStrings = {"day", "campaign", "imps", "conversions"};
        String output = "";
    
        for (int i = 0; i < listOfStrings.length; i++) {
            output += "\"" + listOfStrings[i] + "\"";
            if (i != listOfStrings.length - 1) {
                output += ", ";
            }
        }
    
        System.out.println(output);
    }
    

    Output: "day", "campaign", "imps", "conversions"

    0 讨论(0)
  • 2020-12-13 00:28

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

    0 讨论(0)
  • 2020-12-13 00:29

    Add the quotes along with the separator and then append the quotes to the front and back.

    "\"" + Joiner.on("\",\"").join(values) + "\""
    
    0 讨论(0)
  • 2020-12-13 00:35

    There is no method present in JDK which can do this, but you can use the Apache Commons Langs StringUtls class , StringUtils.join() it will work

    0 讨论(0)
提交回复
热议问题