实现replace
- 实现方法 replace, 能够替换字符串中的某个部分
- 源代码
package java0119;
public class Replace {
public static void main(String[] args) {
String str = "Hello World";
Replace ex = new Replace();
System.out.println("调用 String 类中的 replace 方法: " + str.replace("World", "nihao"));
System.out.println(" 自己写的 replace 方法: " + myReplace(str, "World", "nihao"));
}
public static String myReplace(String s1, String target, String replacement) {
String result3 = "";
int count3 = 0;
int start = 0;
for (int i = 0; i < s1.length(); i++) {
if (i + target.length() <= s1.length() && target.equals(s1.substring(i, i + target.length()))) {
if (count3 == 0) {
result3 += s1.substring(0, i) + replacement;
start = i + target.length();
count3++;
} else {
result3 += s1.substring(start, i) + replacement;
start = i + target.length();
}
}
}
if (start < s1.length()) {
result3 += s1.substring(start, s1.length());
}
return result3;
}
}
- 运行结果
评论(0)