How to trim the whitespace from a string? [duplicate]
This question already has an answer here: Strip Leading and Trailing Spaces From Java String 6 answers I am writing this function for a J2ME application, so I don't have some of the more advanced / modern Java classes available to me. I am getting java.lang.ArrayIndexOutOfBoundsException on this. So, apparently either it doesn't like the way I've initialized the newChars array, or I'm not doing something correctly when calling System.arraycopy . /* * remove any leading and trailing spaces */ public static String trim(String str) { char[] chars = str.toCharArray(); int len = chars.length; //