Join an array of strings into one delimited string : String Join « Data Type « Java






Join an array of strings into one delimited string

    
/*
  * JBoss, Home of Professional Open Source
  * Copyright 2005, JBoss Inc., and individual contributors as indicated
  * by the @authors tag. See the copyright.txt in the distribution for a
  * full listing of individual contributors.
  *
  * This is free software; you can redistribute it and/or modify it
  * under the terms of the GNU Lesser General Public License as
  * published by the Free Software Foundation; either version 2.1 of
  * the License, or (at your option) any later version.
  *
  * This software 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
  * Lesser General Public License for more details.
  *
  * You should have received a copy of the GNU Lesser General Public
  * License along with this software; if not, write to the Free
  * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
  * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
  */



public class Main{


  /////////////////////////////////////////////////////////////////////////
  //                    Joining/Concatenation Methods                    //
  /////////////////////////////////////////////////////////////////////////

  /**
   * Join an array of strings into one delimited string.
   *
   * @param buff    String buffered used for join (buffer is not reset).
   * @param array   Array of objects to join as strings.
   * @param delim   Delimiter to join strings with or <i>null</i>.
   * @return        Joined string.
   */
  public static String join(final StringBuffer buff, final Object array[],
     final String delim)
  {
     boolean haveDelim = (delim != null);

     for (int i = 0; i < array.length; i++)
     {
        buff.append(array[i]);

        // if this is the last element then don't append delim
        if (haveDelim && (i + 1) < array.length)
        {
           buff.append(delim);
        }
     }

     return buff.toString();
  }

  /**
   * Join an array of strings into one delimited string.
   *
   * @param array   Array of objects to join as strings.
   * @param delim   Delimiter to join strings with or <i>null</i>.
   * @return        Joined string.
   */
  public static String join(final Object array[], final String delim)
  {
     return join(new StringBuffer(), array, delim);
  }

  /**
   * Convert and join an array of objects into one string.
   *
   * @param array   Array of objects to join as strings.
   * @return        Converted and joined objects.
   */
  public static String join(final Object array[])
  {
     return join(array, null);
  }

  /**
   * Convert and join an array of bytes into one string.
   *
   * @param array   Array of objects to join as strings.
   * @return        Converted and joined objects.
   */
  public static String join(final byte array[])
  {
     Byte bytes[] = new Byte[array.length];
     for (int i = 0; i < bytes.length; i++)
     {
        bytes[i] = new Byte(array[i]);
     }

     return join(bytes, null);
  }

  /**
   * Return a string composed of the given array.
   *
   * @param buff       Buffer used to construct string value (not reset).
   * @param array      Array of objects.
   * @param prefix     String prefix.
   * @param separator  Element sepearator.
   * @param suffix     String suffix.
   * @return           String in the format of:
   *                   prefix + n ( + separator + n+i)* + suffix.
   */
  public static String join(final StringBuffer buff, final Object[] array,
     final String prefix, final String separator,
     final String suffix)
  {
     buff.append(prefix);
     join(buff, array, separator);
     buff.append(suffix);

     return buff.toString();
  }

  /**
   * Return a string composed of the given array.
   *
   * @param array      Array of objects.
   * @param prefix     String prefix.
   * @param separator  Element sepearator.
   * @param suffix     String suffix.
   * @return           String in the format of:
   *                   prefix + n ( + separator + n+i)* + suffix.
   */
  public static String join(final Object[] array, final String prefix,
     final String separator, final String suffix)
  {
     return join(new StringBuffer(), array, prefix, separator, suffix);
  }


}

   
    
    
    
  








Related examples in the same category

1.Joins array elements into a single String without specifying the start index and end index.
2.Joins array elements into a single String: specify the start index and end index.
3.Joins array elements: Null objects or empty strings within the array are represented by empty strings.
4.Joins the elements of Collection into a single String with string separator.
5.Joins the elements of Iterator into a single with char value as separator.
6.Joins the elements of the provided Collection into a single String containing the provided elements.
7.Joins the elements of the provided Iterator into a single String containing the provided elements.
8.Joins the elements of the provided array into a single String containing the provided list of elements.
9.Split and join strings
10.Split and join strings 2
11.Join String
12.Concatenates two arrays of strings
13.Remove/collapse multiple spaces.
14.Reverse the split operation.
15.Converts a String array to an String, joined by the Seperator