From 76fa43548d483be63b9aa6c9e31a1ea0d9ad645b Mon Sep 17 00:00:00 2001 From: Marcel van den Boer Date: Fri, 8 Apr 2011 20:31:33 -0600 Subject: [PATCH] Implementation of String.replace(CharSequence, CharSequence) --- classpath/java/lang/String.java | 36 +++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) diff --git a/classpath/java/lang/String.java b/classpath/java/lang/String.java index 788400dbac..7f6ff1edfc 100644 --- a/classpath/java/lang/String.java +++ b/classpath/java/lang/String.java @@ -494,6 +494,42 @@ public final class String public String replaceAll(String regex, String replacement) { return Pattern.compile(regex).matcher(this).replaceAll(replacement); } + + public String replace(CharSequence target, CharSequence replace) { + if (target.length() == 0) { + return this.infuse(replace.toString()); + } + + String targetString = target.toString(); + String replaceString = replace.toString(); + + int targetSize = target.length(); + + StringBuilder returnValue = new StringBuilder(); + String unhandled = this; + + int index = -1; + while ((index = unhandled.indexOf(targetString)) != -1) { + returnValue.append(unhandled.substring(0, index)).append(replaceString); + unhandled = unhandled.substring(index + targetSize, + unhandled.length()); + } + + returnValue.append(unhandled); + return returnValue.toString(); + } + + private String infuse(String infuseWith) { + StringBuilder retVal = new StringBuilder(); + + String me = this; + for (int i = 0; i < me.length(); i++) { + retVal.append(infuseWith).append(me.substring(i, i + 1)); + } + + retVal.append(infuseWith); + return retVal.toString(); + } public native String intern();