Java trimArrayElements() Utility Method - Trim Array Elements

This page contains the source code of Java trimArrayElements() Utility Method - This utility method trim the elements of the given string array.
Well, this utility method internally uses the trim() method.

Java trimArrayElements() Utility Method

/**
 * Trim the elements of the given {@code String} array,
 * calling {@code String.trim()} on each of them.
 * @param array the original {@code String} array
 * @return the resulting array (of the same size) with trimmed elements
 */
public static String[] trimArrayElements( String[] array) {
    if (isEmpty(array)) {
         return new String[0];
    }

    String[] result = new String[array.length];
    for (int i = 0; i < array.length; i++) {
         String element = array[i];
         result[i] = (element != null ? element.trim() : null);
    }
    return result;
}

Related Utility Methods


Comments