replace()

2023. 5. 22. 10:19

✔️ 코플릿을 풀다가 replace() 메서드를 사용할 일이 생겼는데 사용하려고 보니까 replace()를 두 가지로 사용할 수 있었다.

 

  • replace(CharSequence target, CharSequence replacement)
    • 문자열에서 특정 문자열(target)을 다른 문자열(replacement)로 대체하는 데 사용한다.
  • replace(char oldChar, char newChar)
    • 문자열에서 특정 문자(oldChar)를 다른 문자(newChar)로 대체하는 데 사용한다.
String str = "Hello, World!";
String newStr = str.replace('o', 'x');
System.out.println(newStr); // 출력: Hellx, Wxrld!

String str2 = "Java is great!";
String newStr2 = str2.replace("great", "awesome");
System.out.println(newStr2); // 출력: Java is awesome!

'Java > Method' 카테고리의 다른 글

split()  (0) 2023.04.24
str.substring()  (0) 2023.04.22
Math.sqrt()  (0) 2023.04.22
str.replace(char oldChar, char newChar)  (0) 2023.04.22
Character.getNumericValue()  (0) 2023.04.22

BELATED ARTICLES

more