/** * Trims the spaces characters from both ends of of the character array. * @param array the char array to trim * @return the trimmed string * @ibm-api */ public static final String trim( char[] array ) { int count = array.length; int last = count; int st = 0; while ((st < last) && isSpace(array[st])) { st++; } while ((st < last) && isSpace(array[last - 1])) { last--; } if( last>st ) { return new String( array, st, last-st ); } return ""; //$NON-NLS-1$ }
/** * Trims the spaces characters from both ends of of the character array. * @param array the char array to trim * @return the trimmed string * @ibm-api */ public static final String trim( char[] array ) { int count = array.length; int last = count; int st = 0; while ((st < last) && isSpace(array[st])) { st++; } while ((st < last) && isSpace(array[last - 1])) { last--; } if( last>st ) { return new String( array, st, last-st ); } return ""; //$NON-NLS-1$ }
/** * Trims the space characters from the beginning of a string. * For example, the call <CODE>ltrim (" Tennessee")</CODE> * returns the string "Tennessee".<BR> * @param s the string to edit * @return the trimmed string * @ibm-api */ public static final String ltrim(String s) { int count = s.length(); int st = 0; while ((st < count) && isSpace(s.charAt(st))) { st++; } return st>0 ? s.substring( st, count ) : s; } private static boolean isSpace( char c ) {
/** * Trims the space characters from the end of a string. * For example, the call <CODE>rtrim ("Tennessee ")</CODE> * returns the string "Tenness".<BR> * All characters that have codes less than or equal to * <code>'\u0020'</code> (the space character) are considered to be * white space. * @param s the string to edit * @return the trimmed string * @ibm-api */ public static final String rtrim(String s) { int count = s.length(); int len = count; while ((0 < len) && isSpace(s.charAt(len-1)) ) { len--; } return (len < count) ? s.substring(0, len) : s; }
/** * Trims the space characters from the beginning of a string. * For example, the call <CODE>ltrim (" Tennessee")</CODE> * returns the string "Tennessee".<BR> * @param s the string to edit * @return the trimmed string * @ibm-api */ public static final String ltrim(String s) { int count = s.length(); int st = 0; while ((st < count) && isSpace(s.charAt(st))) { st++; } return st>0 ? s.substring( st, count ) : s; } private static boolean isSpace( char c ) {
/** * Trims the space characters from the end of a string. * For example, the call <CODE>rtrim ("Tennessee ")</CODE> * returns the string "Tenness".<BR> * All characters that have codes less than or equal to * <code>'\u0020'</code> (the space character) are considered to be * white space. * @param s the string to edit * @return the trimmed string * @ibm-api */ public static final String rtrim(String s) { int count = s.length(); int len = count; while ((0 < len) && isSpace(s.charAt(len-1)) ) { len--; } return (len < count) ? s.substring(0, len) : s; }
public static void removeEmtyTextNodes(Node node) { NodeList nodeList = node.getChildNodes(); int length = nodeList.getLength(); for(int i=length-1; i>=0; i--) { Node child = nodeList.item(i); if(child.getNodeType()==Node.TEXT_NODE) { Text txt = (Text) child; String data = txt.getData(); if(StringUtil.isSpace(data)) { node.removeChild(child); } } else { removeEmtyTextNodes(child); } } }