Java List Combine combine(List tokens, String separator)

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

Description

combine

License

Apache License

Declaration

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

Method Source Code

//package com.java2s;
//License from project: Apache License 

import java.util.List;
import java.util.Set;

public class Main {
    public static String combine(List<String> tokens, String separator) {
        StringBuffer sb = new StringBuffer();
        boolean isFirst = true;
        for (String token : tokens) {
            if (isFirst)
                isFirst = false;//  ww  w  . j  a v a  2  s  .c  om
            else
                sb.append(separator);
            sb.append(token);
        }
        return sb.toString();
    }

    public static String combine(Set<String> tokens, String separator) {
        StringBuffer sb = new StringBuffer();
        boolean isFirst = true;
        for (String token : tokens) {
            if (isFirst)
                isFirst = false;
            else
                sb.append(separator);
            sb.append(token);
        }
        return sb.toString();
    }

    public static String combine(String[] tokens, String separator,
            String nullvalue) {
        if (tokens == null || tokens.length == 0)
            return "";

        StringBuffer sb = new StringBuffer();
        for (int i = 0; i < tokens.length; i++) {
            if (tokens[i] == null) {
                sb.append(nullvalue);
            } else {
                sb.append(tokens[i]);
            }
            sb.append(separator);
        }
        return sb.substring(0, sb.length() - separator.length());
    }

    public static String combine(String[] tokens, String separator) {
        StringBuffer sb = new StringBuffer();
        boolean isFirst = true;
        for (String token : tokens) {
            if (isFirst)
                isFirst = false;
            else
                sb.append(separator);
            sb.append(token);
        }
        return sb.toString();
    }
}

Related

  1. combine(int sign, List exponentBits, List mantissaBits)
  2. combine(List r, char sep)
  3. combine(List> lists)
  4. combine(List commands, int startAt, int endAt)
  5. combine(List strlist, String delimiter)
  6. combine(List a, List b)
  7. combine(List dest, Collection... src)
  8. combine(List list, int maxK)
  9. combine(List... dests)