String 클래스의 split() 메소드의 매개 타입은 String 문자열이지만, 반환 타입은 String 배열이다.
Arrays.toString() 의 인자로 담아주어야 배열을 출력할 수 있다.
문자열.split(" "): 공백 한 칸을 기준으로 분리한다.
String str = "boo ob";
System.out.println(Arrays.toString(str.split(" ")));
// 출력
"boo", "ob"
문자열.split("o"): 문자열 "o"를 기준으로 분리한다.
첫 번째 "o": "b"와 "o:and:foo"로 분리된다.
두 번째 "o": ""와 ":and:foo"로 분리된다.
세 번째 "o": ":and:f"로 분리된다.
String str = "bo:and:foo";
System.out.println(Arrays.toString(str.split("o")));
// 출력
"b", ":and:f"
여기서는 기준 문자열이 인접하는 경우가 문자열의 끝인데, 처음과 중간에서 인접하는 경우와는 달리 끝에서 인접하면 빈 문자열이 생성되지 않는다. (몇 개가 인접하든 관계없이)
결과적으로, 기준 문자열이 문자열의 시작에 존재하거나 2개가 연속으로 인접해있는 경우에는 빈 문자열("")이 생기고, 끝에 1개 이상 존재하거나 인접하면 해당 기준 문자열이 제거된다.
String str = "boob";
System.out.println(Arrays.toString(str.split("o")));
// 출력
"b", "", "b"
예시: "booob" ( "o"가 총 3개)
String str = "booob";
System.out.println(Arrays.toString(str.split("o")));
// 출력
"b", "", "", "b"