indexof ()
입력 문자열 내에서 지정된 문자열의 첫 번째 발생에 대한 0기반 색인을 보고합니다.
찾아보기 또는 입력 문자열이 string 유형이 아닌 경우 함수는 강제로 값을 string으로 캐스트합니다.
구문
indexof(소스,찾아보기[,start_index[,length[,발생]]])
인수
- source: 입력 문자열입니다.
- lookup: 검색할 문자열입니다.
- start_index: 검색 시작 위치입니다. 음수 값은 abs (start_index) 와 같은 여러 단계를 통해 소스 의 끝에서 시작 검색 위치를 오프셋합니다. 선택사항.
- length: 검사할 문자 위치의 수입니다. -1 값은 무제한 길이를 의미합니다. 선택사항.
- occurrence: 발생 횟수입니다. 기본값은 1입니다. 선택사항.
리턴값
lookup의 0기반 인덱스 위치입니다.
입력에서 문자열을 찾을 수 없는 경우 -1을 리턴합니다.
관련 없는 입력 (occurrence < 0또는 length < -1) 의 경우- null을 리턴합니다.
예
print
idx1 = indexof("abcdefg","cde") // lookup found in input string
, idx2 = indexof("abcdefg","cde",1,4) // lookup found in researched range
, idx3 = indexof("abcdefg","cde",1,2) // search starts from index 1, but stops after 2 chars, so full lookup can't be found
, idx4 = indexof("abcdefg","cde",3,4) // search starts after occurrence of lookup
, idx5 = indexof("abcdefg","cde",-5) // negative start index
, idx6 = indexof(1234567,5,1,4) // two first parameters were forcibly casted to strings "12345" and "5"
, idx7 = indexof("abcdefg","cde",2,-1) // lookup found in input string
, idx8 = indexof("abcdefgabcdefg", "cde", 1, 10, 2) // lookup found in input range
, idx9 = indexof("abcdefgabcdefg", "cde", 1, -1, 3) // the third occurrence of lookup is not in researched range
### 결과| idx1 | idx2 | idx3 | idx4 | idx5 | idx6 | idx7 | idx8 | idx9 |
|---|---|---|---|---|---|---|---|---|
| 2 | 2 | 2 | -1 | 2 | 4 | 2 | 9 | -1 |