str.replace(char oldChar, char newChar)

2023. 4. 22. 16:38

💡 str.replace(char oldChar, char newChar)

➡️ 문자열(String)에서 특정 문자를 다른 문자로 바꾸는 메서드 
➡️ oldChar와 일치하는 모든 문자를 newChar로 바꾸어 새로운 문자열 반환 

String str = "Hello world!"
String newStr = str.replace('o', 'O');

System.out.println(newStr); // -> HellO wOrld!
  • 대소문자 구분 O 
    ➡️ 만약 대소문자를 구분하지 않고 바꾸고 싶다면 replaceAll() 메서드 사용 

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

str.substring()  (0) 2023.04.22
Math.sqrt()  (0) 2023.04.22
Character.getNumericValue()  (0) 2023.04.22
Arrays.toString() / Arrays.deepToString()  (0) 2023.04.21
length / length()  (0) 2023.04.20

BELATED ARTICLES

more