예. String.replaceAll()방법 을 사용 하는 것은 매우 간단합니다 .
package com.test;
public class Replace {
public static void main(String[] args) {
String input = "Hello World";
input = input.replaceAll("o", "0");
System.out.println(input);
}
}
산출:
Hell0 W0rld
StringBuilder.replace(int start, int end, String str)대신 사용하고 싶다면 여기로 가십시오.
public static void main(String args[]) {
StringBuilder sb = new StringBuilder("This is a new StringBuilder");
System.out.println("Before: " + sb);
String from = "new";
String to = "replaced";
sb = sb.replace(sb.indexOf(from), sb.indexOf(from) + from.length(), to);
System.out.println("After: " + sb);
}
산출:
Before: This is a new StringBuilder
After: This is a replaced StringBuilder