Joins the elements of the provided Iterator into a single String containing the provided elements. : String Join « Data Type « Java Tutorial






/*
 * 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.ArrayList;
import java.util.Iterator;
import java.util.List;

public class Main {

  /**
   * Joins the elements of the provided <code>Iterator</code> into
   * a single String containing the provided elements.
   *
   * No delimiter is added before or after the list.
   * A <code>null</code> separator is the same as an empty String ("").
   *
   * See the examples here: {@link #join(Object[],String)}. 
   *
   * @param iterator  the <code>Iterator</code> of values to join together, may be null
   * @param separator  the separator character to use, null treated as ""
   * @return the joined String, <code>null</code> if null iterator input
   */
  public static String join(Iterator iterator, String separator) {

      // handle null, zero and one elements before building a buffer
      if (iterator == null) {
          return null;
      }
      if (!iterator.hasNext()) {
          return "";
      }
      Object first = iterator.next();
      if (!iterator.hasNext()) {
          return toString(first);
      }

      // two or more elements
      StringBuffer buf = new StringBuffer(256); // Java default is 16, probably too small
      if (first != null) {
          buf.append(first);
      }

      while (iterator.hasNext()) {
          if (separator != null) {
              buf.append(separator);
          }
          Object obj = iterator.next();
          if (obj != null) {
              buf.append(obj);
          }
      }
      return buf.toString();
  }
  // ToString
  //-----------------------------------------------------------------------
  /**
   * Gets the <code>toString</code> of an <code>Object</code> returning
   * an empty string ("") if <code>null</code> input.
   * 
   * <pre>
   * ObjectUtils.toString(null)         = ""
   * ObjectUtils.toString("")           = ""
   * ObjectUtils.toString("bat")        = "bat"
   * ObjectUtils.toString(Boolean.TRUE) = "true"
   * </pre>
   * 
   * @see StringUtils#defaultString(String)
   * @see String#valueOf(Object)
   * @param obj  the Object to <code>toString</code>, may be null
   * @return the passed in Object's toString, or nullStr if <code>null</code> input
   * @since 2.0
   */
  public static String toString(Object obj) {
      return obj == null ? "" : obj.toString();
  }

}








2.33.String Join
2.33.1.Join String
2.33.2.Join an array of strings into one delimited string
2.33.3.Join string from soapUI
2.33.4.Joins array elements into a single String without specifying the start index and end index.
2.33.5.Joins array elements into a single String: specify the start index and end index.
2.33.6.Joins array elements: Null objects or empty strings within the array are represented by empty strings.
2.33.7.Joins the elements of Collection into a single String with string separator.
2.33.8.Joins the elements of the provided Collection into a single String containing the provided elements.
2.33.9.Joins the elements of the provided Iterator into a single String containing the provided elements.
2.33.10.Joins the elements of the provided array into a single String containing the provided list of elements.
2.33.11.Overlays part of a String with another String.