https://school.programmers.co.kr/learn/courses/30/lessons/120826
문자열 my_string
과 문자 letter
이 매개변수로 주어집니다. my_string
에서 letter
를 제거한 문자열을 return하도록 solution 함수를 완성해주세요.
1 ≤ my_string의 길이 ≤ 100
letter은 길이가 1인 영문자입니다.
my_string과 letter은 알파벳 대소문자로 이루어져 있습니다.
대문자와 소문자를 구분합니다.
my_string | letter | result |
---|---|---|
"abcdef" | "f" | "abcde" |
"BCBdbe" | "B" | "Cdbe" |
"abcdef" 에서 "f"를 제거한 "abcde"를 return합니다.
"BCBdbe" 에서 "B"를 모두 제거한 "Cdbe"를 return합니다.
class Solution {
public String solution(String my_string, String letter) {
String answer = "";
answer=my_string.replace(letter,"");
return answer;
}
}
replace
함수를 통해 쉽게 계산할 수 있다.
함수를 사용하지 않으려고 작성했던 코드가 있다.
class Solution {
public String solution(String my_string, String letter) {
String answer = "";
char[] my_char=new char[my_string.length()];
my_char=my_string.toCharArray();
for(int i=0;i<my_char.length;i++){
if(my_char[i]==letter.charAt(0)){
my_string.replace(letter.charAt(0),"");
}
}
answer=String.valueOf(my_char);
return answer;
}
}
왜 char형은 공백이 안되는가...
string으로 변환해야한다고 하기도 하고
그래서 그냥 함수를 사용했다.