Tweak deleteWhitespace to avoid creating a new String if possible

git-svn-id: https://svn.apache.org/repos/asf/jakarta/commons/proper/lang/trunk@137685 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Stephen Colebourne 2003-10-21 20:24:22 +00:00
parent 25027ea901
commit 25a4549ceb
1 changed files with 10 additions and 6 deletions

View File

@ -145,7 +145,7 @@ import java.util.List;
* @author Gary Gregory
* @author Phil Steitz
* @since 1.0
* @version $Id: StringUtils.java,v 1.109 2003/09/07 14:32:34 psteitz Exp $
* @version $Id: StringUtils.java,v 1.110 2003/10/21 20:24:22 scolebourne Exp $
*/
public class StringUtils {
// Performance testing notes (JDK 1.4, Jul03, scolebourne)
@ -2439,17 +2439,21 @@ public class StringUtils {
* @return the String without whitespaces, <code>null</code> if null String input
*/
public static String deleteWhitespace(String str) {
if (str == null) {
return null;
if (str == null || str.length() == 0) {
return str;
}
int sz = str.length();
StringBuffer buffer = new StringBuffer(sz);
char[] chs = new char[sz];
int count = 0;
for (int i = 0; i < sz; i++) {
if (!Character.isWhitespace(str.charAt(i))) {
buffer.append(str.charAt(i));
chs[count++] = str.charAt(i);
}
}
return buffer.toString();
if (count == sz) {
return str;
}
return new String(chs, 0, count);
}
// Replacing