Java: convert List to a String

前端 未结 22 2575
日久生厌
日久生厌 2020-11-22 01:03

JavaScript has Array.join()

js>[\"Bill\",\"Bob\",\"Steve\"].join(\" and \")
Bill and Bob and Steve

Does Java have anything

相关标签:
22条回答
  • 2020-11-22 01:34

    You can do this:

    String aToString = java.util.Arrays.toString(anArray);
    // Do not need to do this if you are OK with '[' and ']'
    aToString = aToString.substring(1, aToString.length() - 1);
    

    Or a one-liner (only when you do not want '[' and ']')

    String aToString = java.util.Arrays.toString(anArray).substring(1).replaceAll("\\]$", "");

    Hope this helps.

    0 讨论(0)
  • 2020-11-22 01:34

    A fun way to do it with pure JDK, in one duty line:

    String[] array = new String[] { "Bill", "Bob", "Steve","[Bill]","1,2,3","Apple ][" };
    String join = " and ";
    
    String joined = Arrays.toString(array).replaceAll(", ", join)
            .replaceAll("(^\\[)|(\\]$)", "");
    
    System.out.println(joined);
    

    Output:

    Bill and Bob and Steve and [Bill] and 1,2,3 and Apple ][


    A not too perfect & not too fun way!

    String[] array = new String[] { "7, 7, 7","Bill", "Bob", "Steve", "[Bill]",
            "1,2,3", "Apple ][" };
    String join = " and ";
    
    for (int i = 0; i < array.length; i++) array[i] = array[i].replaceAll(", ", "~,~");
    String joined = Arrays.toString(array).replaceAll(", ", join)
            .replaceAll("(^\\[)|(\\]$)", "").replaceAll("~,~", ", ");
    
    System.out.println(joined);
    

    Output:

    7, 7, 7 and Bill and Bob and Steve and [Bill] and 1,2,3 and Apple ][

    0 讨论(0)
  • 2020-11-22 01:37

    I wrote this one (I use it for beans and exploit toString, so don't write Collection<String>):

    public static String join(Collection<?> col, String delim) {
        StringBuilder sb = new StringBuilder();
        Iterator<?> iter = col.iterator();
        if (iter.hasNext())
            sb.append(iter.next().toString());
        while (iter.hasNext()) {
            sb.append(delim);
            sb.append(iter.next().toString());
        }
        return sb.toString();
    }
    

    but Collection isn't supported by JSP, so for TLD I wrote:

    public static String join(List<?> list, String delim) {
        int len = list.size();
        if (len == 0)
            return "";
        StringBuilder sb = new StringBuilder(list.get(0).toString());
        for (int i = 1; i < len; i++) {
            sb.append(delim);
            sb.append(list.get(i).toString());
        }
        return sb.toString();
    }
    

    and put to .tld file:

    <?xml version="1.0" encoding="UTF-8"?>
    <taglib version="2.1" xmlns="http://java.sun.com/xml/ns/javaee"
        <function>
            <name>join</name>
            <function-class>com.core.util.ReportUtil</function-class>
            <function-signature>java.lang.String join(java.util.List, java.lang.String)</function-signature>
        </function>
    </taglib>
    

    and use it in JSP files as:

    <%@taglib prefix="funnyFmt" uri="tag:com.core.util,2013:funnyFmt"%>
    ${funnyFmt:join(books, ", ")}
    
    0 讨论(0)
  • 2020-11-22 01:38

    You might want to try Apache Commons StringUtils join method:

    http://commons.apache.org/lang/api/org/apache/commons/lang/StringUtils.html#join(java.util.Iterator, java.lang.String)

    I've found that Apache StringUtils picks up jdk's slack ;-)

    0 讨论(0)
  • 2020-11-22 01:41

    With java 1.8 stream can be used ,

    import java.util.Arrays;
    import java.util.List;
    import java.util.stream.Collectors;
    List<String> list = Arrays.asList("Bill","Bob","Steve").
    String str = list.stream().collect(Collectors.joining(" and "));
    
    0 讨论(0)
  • 2020-11-22 01:42

    You can use the apache commons library which has a StringUtils class and a join method.

    Check this link: https://commons.apache.org/proper/commons-lang/javadocs/api.2.0/org/apache/commons/lang/StringUtils.html

    Note that the link above may become obsolete over time, in which case you can just search the web for "apache commons StringUtils", which should allow you to find the latest reference.

    (referenced from this thread) Java equivalents of C# String.Format() and String.Join()

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