Here you can find the source of join(String[] strings, String glue)
Parameter | Description |
---|---|
strings | an array of String s to be joined together |
glue | the String to use in between elements of the array |
public static String join(String[] strings, String glue)
//package com.java2s; /* /*from w ww . j a v a 2 s .c om*/ * Copyright (C) 2011 Andrew Oberstar. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program. If not, see <http://www.gnu.org/licenses/>. */ import java.util.List; public class Main { /** * Joins a {@code String[]} using {@code glue} to separate * the elements of the array. * @param strings an array of {@code String}s to be joined together * @param glue the {@code String} to use in between elements of the array * @return a {@code String} of the combined contents of the array. */ public static String join(String[] strings, String glue) { StringBuilder builder = new StringBuilder(); for (int i = 0; i < strings.length; i++) { builder.append(strings[i]); if (i != strings.length - 1) { builder.append(glue); } } return builder.toString(); } /** * Joins a {@code List<String>} using {@code glue} to separate * the elements of the array. * * @param strings an array of {@code String}s to be joined together * @param glue the {@code String} to use in between elements of the array * @return a {@code String} of the combined contents of the array. */ public static String join(List<String> strings, String glue) { StringBuilder builder = new StringBuilder(); for (int i = 0; i < strings.size(); i++) { builder.append(strings.get(i)); if (i != strings.size() - 1) { builder.append(glue); } } return builder.toString(); } }