split 메소드는 token을 기준으로 문자열을 슬라이싱 한다.

반환값은 String[] 이다.

public class Sample3 {
    public static void main(String[] args) {
        // String
        // 문자열 자르기
        // Split - array, 제어문

        String str = "AA-BB-CC-DD";               // -는 token임.
        String human = "홍길동-24-172.1-서울시";


        String[] msg = str.split("-");      // split 메소드의 리턴값은 문자열의 배열임
        String[] msg2 = human.split("-");

        System.out.println(Arrays.toString(msg));
        System.out.println(Arrays.toString(msg2));
        // Integer.parseInt(msg[1])
        // Integer.parseDouble(msg[2])

    }
}

 

실행 결과

+ Recent posts