Java List Join join(List tokens, String separator)

Here you can find the source of join(List tokens, String separator)

Description

Joins the set of tokens in the provided list into a single string.

License

Apache License

Parameter

Parameter Description
tokens List of strings to join.
separator Separator to insert between individual strings.

Return

String of the form [token1][separator][token2][separator]...[tokenN]

Declaration

public static String join(List<String> tokens, String separator) 

Method Source Code

//package com.java2s;
/*//from w w  w  .  jav a2  s  .com
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

import java.util.List;

public class Main {
    /**
     * Joins the set of tokens in the provided list into a single string.
     * This method is a shorthand for {@link Util#join(List, String, String, String)}.
     * 
     * @param tokens List of strings to join.
     * @param separator Separator to insert between individual strings.
     * @return String of the form <code>[token1][separator][token2][separator]...[tokenN]</code>
     */
    public static String join(List<String> tokens, String separator) {
        return (join(tokens, null, null, separator));
    }

    /**
     * Joins the set of tokens in the provided list into a single string.
     * 
     * Any empty strings or <code>null</code> entries in the <code>tokens</code> list 
     * will be removed to achieve a better resulting joined string.
     * 
     * @param tokens List of strings to join.
     * @param prefix String to prepend to each token.
     * @param suffix String to append to each token.
     * @param separator Separator to insert between individual strings.
     * @return String of the form <code>[prefix][token1][suffix][separator][prefix][token2][suffix][separator]...[prefix][tokenN][suffix]</code>
     *         <br/><br/>
     *         Example: call <code>join(["cat","sat","on","the","mat"], "[", "]", ", ")</code> results in the following output:
     *                  <code>"[cat], [sat], [on], [the], [mat]"</code>
     */
    public static String join(List<String> tokens, String prefix, String suffix, String separator) {
        if (tokens == null) {
            // nothing to join
            return (null);
        }

        // list of strings is not empty, but some pre-processing is necessary
        // to remove any empty strings that may be there
        for (int i = tokens.size() - 1; i >= 0; i--) {
            if (tokens.get(i) == null || tokens.get(i).length() == 0) {
                tokens.remove(i);
            }
        }

        // now start the actual processing, but it may be the case that all strings
        // were empty and we now have an empty list
        if (tokens.isEmpty()) {
            // nothing to join - just return an empty string
            return ("");
        } else {
            // there are some tokens -- perform the joining
            String effectivePrefix = (prefix == null ? "" : prefix);
            String effectiveSuffix = (suffix == null ? "" : suffix);
            String effectiveSeparator = (separator == null ? "" : separator);

            StringBuilder result = new StringBuilder();
            for (int i = 0; i < tokens.size(); i++) {
                result.append(effectivePrefix + tokens.get(i) + effectiveSuffix);
                result.append(i == tokens.size() - 1 ? "" : effectiveSeparator);
            }

            return (result.toString());
        }
    }
}

Related

  1. join(List strings, String separator)
  2. join(List stringsToJoin, char delimiter)
  3. join(List strList, char separator)
  4. join(List strList, String sep)
  5. join(List tokens, String sep)
  6. join(List tokens, String separator)
  7. join(List values)
  8. join(List values, String delimiter)
  9. join(List values, String string)