split() 메서드의 두번째 파라미터에 limit 값이 들어간다.
양수인 경우 지정된 크기만큼 값이 나눠지고, 음수인 경우 마지막 공백도 포함해서 모든 값을 나눠 배열로 반환한다.
limit를 사용하지 않고 split() 메서드를 사용하면, 구분한 문자열의 뒷부분 공백들은 사라진다.
코드
String[] splitString = myString.split("x", -1);
결과
"oxooxoxxox" -> "o", "oo", "o", "", "o", ""
split() 메서드 설명
The limit parameter controls the number of times the pattern is applied and therefore affects the length of the resulting array. If the limit n is greater than zero then the pattern will be applied at most n - 1 times, the array's length will be no greater than n, and the array's last entry will contain all input beyond the last matched delimiter. If n is non-positive then the pattern will be applied as many times as possible and the array can have any length. If n is zero then the pattern will be applied as many times as possible, the array can have any length, and trailing empty strings will be discarded.
참고
'TIL' 카테고리의 다른 글
슬기의 TIL - 2023.06.12 ( AssertJ ) (0) | 2023.06.13 |
---|---|
슬기의 TIL - 2023.06.11 ( console에 log 찍기 + Scheduled ) (0) | 2023.06.12 |
슬기의 TIL - 2023.06.10 (0) | 2023.06.12 |
슬기의 TIL - 2023.06.09 (0) | 2023.06.12 |
슬기의 TIL - 2023.06.08 (0) | 2023.06.12 |