You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@commons.apache.org by gg...@apache.org on 2003/12/15 02:51:37 UTC
cvs commit: jakarta-commons/lang/src/java/org/apache/commons/lang StringUtils.java
ggregory 2003/12/14 17:51:36
Modified: lang/src/java/org/apache/commons/lang StringUtils.java
Log:
http://nagoya.apache.org/bugzilla/show_bug.cgi?id=25454
StringUtils.replaceChars(String,String,String).
Revision Changes Path
1.121 +14 -28 jakarta-commons/lang/src/java/org/apache/commons/lang/StringUtils.java
Index: StringUtils.java
===================================================================
RCS file: /home/cvs/jakarta-commons/lang/src/java/org/apache/commons/lang/StringUtils.java,v
retrieving revision 1.120
retrieving revision 1.121
diff -u -r1.120 -r1.121
--- StringUtils.java 29 Nov 2003 13:24:33 -0000 1.120
+++ StringUtils.java 15 Dec 2003 01:51:36 -0000 1.121
@@ -2699,39 +2699,25 @@
* @since 2.0
*/
public static String replaceChars(String str, String searchChars, String replaceChars) {
- if (str == null || str.length() == 0 || searchChars == null || searchChars.length() == 0) {
+ if (StringUtils.isEmpty(str) || StringUtils.isEmpty(searchChars)) {
return str;
}
- char[] chars = str.toCharArray();
- int len = chars.length;
- boolean modified = false;
- for (int i = 0, isize = searchChars.length(); i < isize; i++) {
- char searchChar = searchChars.charAt(i);
- if (replaceChars == null || i >= replaceChars.length()) {
- // delete
- int pos = 0;
- for (int j = 0; j < len; j++) {
- if (chars[j] != searchChar) {
- chars[pos++] = chars[j];
- } else {
- modified = true;
- }
+ if (replaceChars == null) {
+ replaceChars = "";
+ }
+ StringBuffer buffer = new StringBuffer(str.length());
+ for (int i = 0; i < str.length(); i++) {
+ char ch = str.charAt(i);
+ int index = searchChars.indexOf(ch);
+ if (index >= 0) {
+ if (index < replaceChars.length()) {
+ buffer.append(replaceChars.charAt(index));
}
- len = pos;
} else {
- // replace
- for (int j = 0; j < len; j++) {
- if (chars[j] == searchChar) {
- chars[j] = replaceChars.charAt(i);
- modified = true;
- }
- }
+ buffer.append(ch);
}
}
- if (modified == false) {
- return str;
- }
- return new String(chars, 0, len);
+ return buffer.toString();
}
// Overlay
---------------------------------------------------------------------
To unsubscribe, e-mail: commons-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-dev-help@jakarta.apache.org