Java trimArrayElements() Utility Method

Java String Utility Methods


Java trimArrayElements() Utility Method trim the elements of the given String array, calling {@code String.trim()} on each of them.

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;
}


Comments