프로그래머스 java 숫자 문자열과 영단어

jieun·2022년 8월 5일
0

java 코테 공부

목록 보기
3/17

해결방법

  1. 치환하기 위한 영단어 배열 생성 -> number
  2. 문자열에서 대응되는 영단어를 찾아 숫자로 치환해야함 -> s=s.replace
  3. 문자열을 정수로 변환 -> answer

활용코드

replace로 문자열 치환

	s=s.replace('기존문자열', '바꿀문자열');

정수를 문자열로 변환

	Integer.toString(i)

문자열을 정수로 변환

	Integer.parseInt(s);

전체코드

class Solution {
    public int solution(String s) {
        int answer;
        String[] number = {"zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine"};
        for (int i=0; i<10; i++){
            s=s.replaceAll(number[i], Integer.toString(i));
        }
        answer = Integer.parseInt(s);
        return answer;
    }
}
profile
개발새발 블로그

0개의 댓글