Join function in Org.apache.commons.lang.StringUtils

Source: Internet
Author: User

Transfer from http://my.oschina.net/zenglingfan/blog/134872

When writing code, often encounter need to put a list of each element, separated by commas into a string of requirements, used to write a piece of the more ugly code, first put the string out, and then the last redundant comma removed; Although the function can be achieved, but always feel that the last step of the operation is not necessary:

public static string Join (List<string> List, string seperator) {    if (List.isEmpty ()) {        return "";    }        StringBuffer sb = new StringBuffer ();        for (int i=0; i<list.size (); i++) {        sb.append (List.get (i)). Append (Seperator);    }        Return sb.substring (0, Sb.length ()-seperator.length ());}

After going to Java, I never knew that there was a org.apache.commons.lang such a package, which provides a powerful join function, until one time to see colleagues in use ...

Look at the source code, the realization of the idea of a more sophisticated, clever to avoid like me on the above, always need to finally take another substring problem, org.apache.commons.lang.StringUtils in the Join function code as follows:

/** * <p>joins The elements of the provided array into a single String * containing the provided list of ele     ments.</p> * * <p>no delimiter is added before or after the list.      * Null objects or empty strings within the array is represented by * Empty strings.</p> * * <pre> * Stringutils.join (NULL, *) = null * Stringutils.join ([], *) = "" * stringutils  . join ([null], *) = "" * Stringutils.join (["A", "B", "C"], '; ')  = "A;b;c" * Stringutils.join (["A", "B", "C"], NULL) = "ABC" * STRINGUTILS.JOIN ([null, "", "a"], '; ') = ";;  A "* </pre> * * @param array the array of values to join together, could be null * @param separator  The separator character to use * @param startIndex the first index to start joining from. It is * A error to pass in a end index past the end of the array * @param endIndex the index to stop joining fro M (ExclusiVE). It is * A error to pass in an end index past the end of the array * @return the joined String, {@code null} if Nu ll array Input * @since 2.0 */public static String join (object[] array, char separator, int startIndex, int en        Dindex) {if (array = = NULL) {return null;        } int noofitems = Endindex-startindex;        if (noofitems <= 0) {return EMPTY;        } StringBuilder buf = new StringBuilder (Noofitems * 16);             for (int i = StartIndex; i < EndIndex; i++) {if (i > StartIndex) {buf.append (separator);            } if (array[i]! = null) {buf.append (array[i]);    }} return buf.tostring (); }

The essence of the above code is this sentence

if (i > StartIndex) {    buf.append (separator);}

Ensures that the separator is not inserted for the first time, and that the inserted separator is preceded by the value of the array, so that there is no more separator at the end.

Their own code is mainly not thought of StringBuilder directly with toString to get results, so always can not be done directly in the For loop, come out also to do once (also calculate various length), making the code difficult to see.

Join function in Org.apache.commons.lang.StringUtils

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.