.replace() 란?

    /**
     * Replaces each substring of this string that matches the literal target
     * sequence with the specified literal replacement sequence. The
     * replacement proceeds from the beginning of the string to the end, for
     * example, replacing "aa" with "b" in the string "aaa" will result in
     * "ba" rather than "ab".
     *
     * @param  target The sequence of char values to be replaced
     * @param  replacement The replacement sequence of char values
     * @return  The resulting string
     * @since 1.5
     */
    public String replace(CharSequence target, CharSequence replacement) {
	    // ...
    }

.replace(CharSequence target, CharSequence replacement) 메서드는

원본 문자열에서 target으로 지정된 모든 부분 문자열을 찾아 replacement로 지정된 문자열로

교체한 새로운 문자열을 반환한다.

<aside> 💡

Tip. CharSequence는 String, StringBuilder, StringBuffer 등

문자열 시퀀스를 나타내는 인터페이스다.

</aside>

Ex

1. 일반적인 예시

public class ReplaceExample {
	public static void main(String[] args) {
		String originalString = "Lee"; // 원본 문자열
		String targetString = "Le"; // 제거하고 싶은 부분 문자열
		String replacement = ""; // 대체할 문자열
		
		// replace() 메서드를 사용하여 부분 문자열 제거
		String result = originalString.replace(targetString, replacement);
		
			System.out.println("원본 문자열: " + originalString);
      System.out.println("제거할 부분 문자열: " + targetString);
      System.out.println("결과 문자열: " + resultString);
	}
}
원본 문자열: Lee
제거할 부분 문자열: Le
결과 문자열: e

2. 부분 문자열이 없는 경우

public class ReplaceExample {
	public static void main(String[] args) {
		String originalString = "Tea"; // 원본 문자열
		String targetString = "Le"; // 제거하고 싶은 부분 문자열
		String replacement = ""; // 대체할 문자열
		
		// replace() 메서드를 사용하여 부분 문자열 제거
		String result = originalString.replace(targetString, replacement);
		
			System.out.println("원본 문자열: " + originalString);
      System.out.println("제거할 부분 문자열: " + targetString);
      System.out.println("결과 문자열: " + resultString);
	}
}
원본 문자열: Tea
제거할 부분 문자열: Le
결과 문자열: e