Java array join int[] array to String

Description

Java array join int[] array to String


//package com.demo2s;

public class Main {
    public static void main(String[] argv) throws Exception {
        int[] tokens = new int[] { 34, 35, 36, 37, 37, 37, 67, 68, 69 };
        String delimiter = "";
        System.out.println(joinInts(tokens, delimiter));
    }/*ww w.  ja  v a  2 s  .co  m*/

    /**
     * Concatenates the given int[] array into one String, inserting a delimiter
     * between each pair of elements.
     */
    public static String joinInts(int[] tokens, String delimiter) {
        if (tokens == null)
            return "";
        StringBuilder result = new StringBuilder();

        for (int i = 0; i < tokens.length; i++) {
            if (i > 0 && delimiter != null) {
                result.append(delimiter);
            }
            result.append(String.valueOf(tokens[i]));
        }
        return result.toString();
    }
}



PreviousNext

Related