Java tutorial
//package com.java2s; //License from project: Apache License public class Main { /** * Builds a String via the passed String array. If the individual String is null or empty, * it skips it. If the delimiter is empty, skips that too * @param args String array to use * @param delimiter Delimiter to use (IE , or a space or _ or / or | ) * @return Fully completed and written String. Example: * String str = buildAStringFromUnknowns(new String[]{"2016", "07", "04"}, "/"); * str would print as: "2016/07/04" */ public static String buildAStringFromUnknowns(String[] args, String delimiter) { StringBuilder sb = new StringBuilder(); sb.append(""); //So that it will always return something try { for (int i = 0; i < args.length; i++) { String str = args[i]; //Boot out nulls and blanks if (str == null) { continue; } if (str.equalsIgnoreCase("")) { continue; } sb.append(str); if (i < args.length - 1) { boolean checkNext = true; try { String str1 = args[(i + 1)]; if (str1 == null) { checkNext = false; } else { if (str1.isEmpty()) { checkNext = false; } } } catch (Exception e) { } if (checkNext) { //Format via delimiter if (delimiter != null) { sb.append(delimiter); } } } } } catch (Exception e) { } return sb.toString(); } }