Study with me/프로그래머스 L0 마스터하기
프로그래머스 - L0 특정한문자를대문자로바꾸기
외계나무
2023. 12. 6. 11:01
프로그래머스 - level 0 특정한 문자를 대문자로 바꾸기
charAt과 toUpperCase으로 길어진 코드...
class Solution {
public String solution(String my_string, String alp) {
String answer = "";
for(int i=0; i<my_string.length(); i++) {
answer += my_string.charAt(i) == alp.charAt(0) ? Character.toUpperCase(my_string.charAt(i)) : my_string.charAt(i);
}
return answer;
}
}
그리고 짧은 해결책.... 힝9
class Solution {
public String solution(String my_string, String alp) {
String a = alp.toUpperCase();
return my_string.replaceAll(alp, a);
// 혹은 한 줄로,
// return my_string.replace(alp, alp.toUpperCase());
}
}