Java Array Join join(String[] tokens, String delimiter)

Here you can find the source of join(String[] tokens, String delimiter)

Description

join

License

Open Source License

Declaration

public static String join(String[] tokens, String delimiter) 

Method Source Code


//package com.java2s;
//License from project: Open Source License 

import java.util.*;

public class Main {
    public static String join(String[] tokens, String delimiter) {
        if (tokens == null) {
            return "null";
        }/*from   www  . ja v a2 s  .co  m*/
        if (tokens.length == 0) {
            return "";
        }
        StringBuilder sb = new StringBuilder();
        sb.append(tokens[0]);
        for (int i = 1; i < tokens.length; i++)
            sb.append(delimiter).append(tokens[i]);
        return sb.toString();
    }

    public static String join(String[] tokens, String delimiter, int start, int end) {
        if (tokens == null) {
            return "null";
        }
        if (tokens.length == 0) {
            return "";
        }
        StringBuilder sb = new StringBuilder();
        sb.append(tokens[start]);
        for (int i = start + 1; i < end; i++)
            sb.append(delimiter).append(tokens[i]);
        return sb.toString();
    }

    public static String join(String delimiter, Object... tkns) {
        StringBuilder sb = new StringBuilder();
        for (Object t : tkns) {
            sb.append(t.toString()).append(delimiter);
        }
        return sb.toString();
    }

    public static String join(String[] tokens, String delimiter, int[] position) {
        if (tokens == null) {
            return "null";
        }
        if (tokens.length == 0) {
            return "";
        }
        StringBuilder sb = new StringBuilder();
        sb.append(tokens[0]);
        position[0] = 0;
        for (int i = 1; i < tokens.length; i++) {
            position[i] = sb.length() + delimiter.length();
            sb.append(delimiter).append(tokens[i]);
        }
        return sb.toString();
    }

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

Related

  1. join(String[] strings, String separator)
  2. join(String[] strings, String seperator)
  3. join(String[] strParts, String glue)
  4. join(String[] strs)
  5. join(String[] strs, String glue, int start, int end)
  6. join(String[] tokens, String separator)
  7. join(String[] x)
  8. join(String[]... arrays)
  9. join(String[]... values)