Join an array of Strings together. : Array « Date Type « Android






Join an array of Strings together.

  
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

class Main {
    /**
     * Join an array of Strings together.
     * 
     * @param glue Token to place between Strings.
     * @param pieces Array of Strings to join.
     * @return String presentation of joined Strings.
     * @see #join(String,java.util.Iterator)
     */
    public static String join(List<String> pieces, String glue) {
        return join(pieces.iterator(), glue);
    }
    /**
     * Join an Iteration of Strings together.
     * <p/>
     * <h5>Example</h5>
     * <p/>
     * <p/>
     * 
     * <pre>
     * // get Iterator of Strings (&quot;abc&quot;,&quot;def&quot;,&quot;123&quot;);
     * Iterator i = getIterator();
     * out.print(TextUtils.join(&quot;, &quot;, i));
     * // prints: &quot;abc, def, 123&quot;
     * </pre>
     * 
     * @param glue Token to place between Strings.
     * @param pieces Iteration of Strings to join.
     * @return String presentation of joined Strings.
     */
    private static String join(Iterator<String> pieces, String glue) {
        StringBuilder s = new StringBuilder();
        while (pieces.hasNext()) {
            s.append(pieces.next());

            if (pieces.hasNext()) {
                s.append(glue);
            }
        }
        return s.toString();
    }
    /**
     * Join an array of Strings together.
     * 
     * @param glue Token to place between Strings.
     * @param pieces Array of Strings to join.
     * @return String presentation of joined Strings.
     * @see #join(String,java.util.Iterator)
     */
    public static String join(Map<String, String> pieces, String glue) {
        List<String> tmp = new ArrayList<String>(pieces.size());
        for (Map.Entry<String, String> entry : pieces.entrySet()) {
            tmp.add(entry.getKey() + ":" + entry.getValue());
        }
        return join(tmp, glue);
    }
}

   
    
  








Related examples in the same category

1.Load string array value from strings.xml
2.Returns a clone of the specified array.
3.Tests two arrays for equality.
4.Transforms a list of Float values into an array of float.
5.Split the string into an array of strings using one of the separator in 'sep'.
6.Int array to HashSet
7.Array Iterator
8.Convert an array of floats to 16.16 fixed-point
9.Convert an array of 16.16 fixed-point values to floating point
10.Reverse an array
11.Using android.util.SparseArray
12.Compare Arrays
13.Does Array contain a certain item
14.Create Array with Unique Value
15.Resize a Java array
16.Get Node value and save to ArrayList
17.read the photo file into a byte array
18.byte Array To Int
19.load 16 Bit PCM Raw Data File As Double Array
20.convert From Short Array To Double Array
21.convert From Double Array To Short Array
22.get Set From Array
23.Convert an array of floats to 16.16 fixed-point 2
24.int To Byte Array 4
25.Convert a byte array to a boolean array. Bit 0 is represented with false, Bit 1 is represented with 1
26.Return a subarray of the byte array in parameter.