Java Array Join join(String[] array, String separator)

Here you can find the source of join(String[] array, String separator)

Description

join

License

Open Source License

Declaration

public static final String join(String[] array, String separator) 

Method Source Code

//package com.java2s;
/*//from w  ww . jav  a2  s  .  co  m
 * Copyright (c) 2011-2018, Meituan Dianping. All Rights Reserved.
 *
 * 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.Collection;

public class Main {
    public static final String join(Collection<String> list, String separator) {
        StringBuilder sb = new StringBuilder(1024);
        boolean first = true;

        for (String item : list) {
            if (first) {
                first = false;
            } else {
                sb.append(separator);
            }

            sb.append(item);
        }

        return sb.toString();
    }

    public static final String join(double[] count, char spit) {
        boolean first = true;
        StringBuilder sb = new StringBuilder(128);

        for (double i : count) {
            if (first) {
                sb.append(i);
                first = false;
            } else {
                sb.append(spit).append(i);
            }
        }
        return sb.toString();
    }

    public static final String join(int[] count, char spit) {
        boolean first = true;
        StringBuilder sb = new StringBuilder(128);

        for (int i : count) {
            if (first) {
                sb.append(i);
                first = false;
            } else {
                sb.append(spit).append(i);
            }
        }
        return sb.toString();
    }

    public static final String join(long[] count, char spit) {
        boolean first = true;
        StringBuilder sb = new StringBuilder(128);

        for (long i : count) {
            if (first) {
                sb.append(i);
                first = false;
            } else {
                sb.append(spit).append(i);
            }
        }
        return sb.toString();
    }

    public static final String join(String[] array, String separator) {
        StringBuilder sb = new StringBuilder(1024);
        boolean first = true;

        for (String item : array) {
            if (first) {
                first = false;
            } else {
                sb.append(separator);
            }

            sb.append(item);
        }

        return sb.toString();
    }
}

Related

  1. join(String[] array, String delim)
  2. join(String[] array, String delimiter)
  3. join(String[] array, String delimiter)
  4. join(String[] array, String delimiter)
  5. join(String[] array, String sep)
  6. join(String[] array, String separator)
  7. join(String[] array, String separator)
  8. join(String[] arrayOfString)
  9. join(String[] datalines)