깜놀하는 해므찌로

Java indexOF 활용 / split 함수 내부 로직 구현 예시 본문

IT

Java indexOF 활용 / split 함수 내부 로직 구현 예시

agnusdei1207 2022. 7. 18. 08:07
반응형
SMALL
public static String[] split(final String source, final String separator) throws NullPointerException {
    String[] returnVal = null;
    int cnt = 1;

    int index = source.indexOf(separator);
    int index0 = 0;
    while (index >= 0) {
        cnt++; 
        index = source.indexOf(separator, index + 1); // 찾을 문자, 시작할 인덱스 위치
    }
    returnVal = new String[cnt];
    cnt = 0;
    index = source.indexOf(separator);
    while (index >= 0) {
        returnVal[cnt] = source.substring(index0, index);
        index0 = index + 1;
        index = source.indexOf(separator, index + 1); 
        cnt++;
    }   
    returnVal[cnt] = source.substring(index0);
    return returnVal;
}

1. indexOF(찾을 문자, 시작할 인덱스 위치) : 인덱스 위치 생략 시 0번째 (처음부터) 탐색

2. split 함수 내부 로직

반응형
LIST