Revision: 14067
Updated Code
at May 18, 2009 10:08 by monkey103
Updated Code
/* instead of Pattern p = Pattern.compile("[\\s]+$"); // start timing p.matcher(str).replaceAll(""); The alternative method below averages 400ns, compared to 3500ns of the regex above */ public static String trimEnd(String str) { int len = str.length(); char[] val = str.toCharArray(); while ((0 < len) && (val[len - 1] <= ' ')) { len--; } return (len < str.length()) ? str.substring(0, len) : str; } /* Similarly, using a StringCharacterIterator is also faster (although memory usage is worse) - averages 500ns */ public static String trimEnd(String str) { StringCharacterIterator iter = new StringCharacterIterator(str); int len = str.length(); for (char c = iter.last(); c != CharacterIterator.DONE && c <= ' '; c = iter .previous()) { len --; } return ((len < str.length())) ? str.substring(0, len) : str; }
Revision: 14066
Updated Code
at May 18, 2009 10:07 by monkey103
Updated Code
/* instead of Pattern p = Pattern.compile("[\\s]+$"); // start timing p.matcher(str).replaceAll(""); The alternative method below averages 400ns, compared to the regex above*/ public static String trimEnd(String str) { int len = str.length(); char[] val = str.toCharArray(); while ((0 < len) && (val[len - 1] <= ' ')) { len--; } return (len < str.length()) ? str.substring(0, len) : str; } /* Similarly, using a StringCharacterIterator is also faster (although memory usage is worse) - averages 500ns */ public static String trimEnd(String str) { StringCharacterIterator iter = new StringCharacterIterator(str); int len = str.length(); for (char c = iter.last(); c != CharacterIterator.DONE && c <= ' '; c = iter .previous()) { len --; } return ((len < str.length())) ? str.substring(0, len) : str; }
Revision: 14065
Updated Code
at May 18, 2009 10:06 by monkey103
Updated Code
/* instead of Pattern p = Pattern.compile("[\\s]+$"); // start timing p.matcher(str).replaceAll(""); This alternative method averages 400ns */ public static String trimEnd(String str) { int len = str.length(); char[] val = str.toCharArray(); while ((0 < len) && (val[len - 1] <= ' ')) { len--; } return (len < str.length()) ? str.substring(0, len) : str; } /* Similarly, using a StringCharacterIterator is also faster (although memory usage is worse) - averages 500ns */ public static String trimEnd(String str) { StringCharacterIterator iter = new StringCharacterIterator(str); int len = str.length(); for (char c = iter.last(); c != CharacterIterator.DONE && c <= ' '; c = iter .previous()) { len --; } return ((len < str.length())) ? str.substring(0, len) : str; }
Revision: 14064
Updated Code
at May 18, 2009 09:52 by monkey103
Updated Code
/* instead of Pattern p = Pattern.compile("[\\s]+$").matcher(str).replaceAll("") - averages 400ns */ public static String trimEnd(String str) { int len = str.length(); char[] val = str.toCharArray(); while ((0 < len) && (val[len - 1] <= ' ')) { len--; } return (len < str.length()) ? str.substring(0, len) : str; } /* Similarly, using a StringCharacterIterator is also faster (although memory usage is worse) - averages 500ns */ public static String trimEnd(String str) { StringCharacterIterator iter = new StringCharacterIterator(str); int len = str.length(); for (char c = iter.last(); c != CharacterIterator.DONE && c <= ' '; c = iter .previous()) { len --; } return ((len < str.length())) ? str.substring(0, len) : str; }
Revision: 14063
Updated Code
at May 18, 2009 09:51 by monkey103
Updated Code
/* instead of Pattern p = Pattern.compile("[\\s]+$").matcher(str).replaceAll("") - averages 300ns */ public static String trimEnd(String str) { int len = str.length(); char[] val = str.toCharArray(); while ((0 < len) && (val[len - 1] <= ' ')) { len--; } return (len < str.length()) ? str.substring(0, len) : str; } /* Similarly, using a StringCharacterIterator is also ~6x faster (although memory usage is worse) - avgs 400ns */ public static String trimEnd(String str) { StringCharacterIterator iter = new StringCharacterIterator(str); int len = str.length(); for (char c = iter.last(); c != CharacterIterator.DONE && c <= ' '; c = iter .previous()) { len --; } return ((len < str.length())) ? str.substring(0, len) : str; }
Revision: 14062
Updated Code
at May 18, 2009 09:48 by monkey103
Updated Code
/* instead of Pattern p = Pattern.compile("[\\s]+$").matcher(str).replaceAll("") */ public static String trimEnd(String str) { int len = str.length(); char[] val = str.toCharArray(); while ((0 < len) && (val[len - 1] <= ' ')) { len--; } return (len < str.length()) ? str.substring(0, len) : str; } /* Similarly, using a StringCharacterIterator is also ~6x faster (although memory usage is worse) */ public static String trimEnd(String str) { StringCharacterIterator iter = new StringCharacterIterator(str); int len = str.length(); for (char c = iter.last(); c != CharacterIterator.DONE && c <= ' '; c = iter .previous()) { len --; } return ((len < str.length())) ? str.substring(0, len) : str; }
Revision: 14061
Updated Code
at May 18, 2009 09:48 by monkey103
Updated Code
/* instead of Pattern p = Pattern.compile("[\\s]+$").matcher(str).replaceAll("") */ public static String trimEnd(String str) { int len = str.length(); char[] val = str.toCharArray(); while ((0 < len) && (val[len - 1] <= ' ')) { len--; } return (len < str.length()) ? str.substring(0, len) : str; } /* Similarly, using a StringCharacterIterator is also ~6x faster (although memory usage is worse */ public static String trimEnd(String str) { StringCharacterIterator iter = new StringCharacterIterator(str); int len = str.length(); for (char c = iter.last(); c != CharacterIterator.DONE && c <= ' '; c = iter .previous()) { len --; } return ((len < str.length())) ? str.substring(0, len) : str; }
Revision: 14060
Updated Code
at May 18, 2009 09:46 by monkey103
Updated Code
// instead of Pattern p = Pattern.compile("[\\s]+$").matcher(str).replaceAll("") public static String trimEnd(String str) { int len = str.length(); char[] val = str.toCharArray(); while ((0 < len) && (val[len - 1] <= ' ')) { len--; } return (len < str.length()) ? str.substring(0, len) : str; }
Revision: 14059
Updated Code
at May 18, 2009 09:44 by monkey103
Updated Code
// instead of Pattern p = Pattern.compile("[\\s]+$").matcher(str).replaceAll("") public static String trimEnd(String str) { int len = str.length(); int st = 0; char[] val = str.toCharArray(); while ((st < len) && (val[st + len - 1] <= ' ')) { len--; } return ((st > 0) || (len < str.length())) ? str.substring(st, len) : str; }
Revision: 14058
Initial Code
Initial URL
Initial Description
Initial Title
Initial Tags
Initial Language
at May 18, 2009 09:38 by monkey103
Initial Code
// instead of Pattern p = Pattern.compile("[\\s]+$").matcher(str).replaceAll("") public static String trimEnd(String str) { int len = str.length(); int st = 0; int off = 0; /* avoid getfield opcode */ char[] val = str.toCharArray(); /* avoid getfield opcode */ while ((st < len) && (val[off + len - 1] <= ' ')) { len--; } return ((st > 0) || (len < str.length())) ? str.substring(st, len) : str; }
Initial URL
Initial Description
Works almost 9x faster than a regex pattern, and memory usage is better (because Matcher objects aren't created).
Initial Title
Trim trailing whitespace
Initial Tags
Initial Language
Java