본문 바로가기

Coding Test/프로그래머스

[프로그래머스/자바] 공백으로 구분하기 2

class Solution {
    public String[] solution(String my_string) {
        String[] temp = my_string.trim().split(" ");
        int n = temp.length;
        for(String s : temp){
            if(s.equals("")) n--;
        }
        String[] result = new String[n];
        int idx = 0;
        for(String s : temp){
            if(!s.equals("")) result[idx++] = s;
        }

        return result;
    }
}

class Solution {
    public String[] solution(String my_string) {
        return my_string.trim().split("[ ]+");
    }
}

  

정규식을 이용한 다른 분의 풀이