Here you can find the source of concatStrings(String[] strs, String delimiter)
Parameter | Description |
---|---|
strs | the given array of String s. |
delimiter | the given delimiter. |
public static String concatStrings(String[] strs, String delimiter)
//package com.java2s; //License from project: Open Source License import java.util.Iterator; public class Main { /**/*from ww w . j a v a 2 s . c om*/ * Concatenates the given collection of {@code String}s with the given delimiter. * * @param strs the given collection of {@code String}s. * @param delimiter the given delimiter. * @return the concatenation. */ public static String concatStrings(Iterable<String> strs, String delimiter) { StringBuilder builder = new StringBuilder(); Iterator<String> iter = strs.iterator(); while (iter.hasNext()) { builder.append(iter.next()); if (iter.hasNext()) builder.append(delimiter); } return builder.toString(); } /** * Concatenates the given array of {@code String}s with the given delimiter. * * @param strs the given array of {@code String}s. * @param delimiter the given delimiter. * @return the concatenation. */ public static String concatStrings(String[] strs, String delimiter) { StringBuilder builder = new StringBuilder(); for (int i = 0; i < strs.length; i++) { builder.append(strs[i]); if (i < strs.length - 1) builder.append(delimiter); } return builder.toString(); } }