StringGetPos

문자열 안에서 지정된 부분문자열의 위치를 열람합니다.

비추천: 이 명령어는 새 스크립트에 사용을 추천하지 않습니다. 대신 InStr 기능을 사용하십시오.

StringGetPos, OutputVar, InputVar, SearchText , Occurrence, Offset

매개변수

OutputVar

The name of the output variable in which to store the retrieved position relative to the first character of InputVar. 위치가 0이면 StringGetPos에 대하여 첫 문자이고 위치가 1이면 InStr()에 대하여 첫 문자입니다.

InputVar

내용이 검색될 입력 변수의 이름. 이름을 퍼센트 사인으로 둘러 싸지 마십시오. 변수의 내용을 이름으로 사용하고 싶다면 예외입니다.

SearchText

검색할 문자열. 부합은 대소문자를 구분하지 않습니다. 단, StringCaseSense가 켜져 있는 경우는 예외입니다.

Occurrence

이 매개변수는 InputVar 안에서 SearchText가 여러 번 출현하면 몇 번째 부합을 발견할지에 영향을 미칩니다. If this parameter is omitted, it defaults to L1 (meaning InputVar will be searched starting from the left for the first match). To change this behavior, specify one of the following options:

Ln: The search will start looking at the left side of InputVar and will continue rightward until the nth match is found.

Rn: The search will start looking at the right side of InputVar and will continue leftward until the nth match is found. If n is omitted (or if Occurrence is 1), it defaults to R1.

예를 들어, 오른쪽에서 네 번째 출현을 발견하고 싶으면. R4를 지정하십시오. 주의: If n is less than or equal to zero, no match will be found.

Offset

왼쪽 끝 또는 오른쪽 끝으로부터 (위의 매개변수에 따라) 건너 뛸 문자의 개수. 생략하면, 기본값은 0입니다. 예를 들어, 다음은 왼쪽 10번째 문자부터 검색을 시작합니다: StringGetPos, OutputVar, InputVar, abc, , 9. 이 매개변수는 표현식일 수 있습니다.

ErrorLevel

ErrorLevel은 지정된 SearchText의 출현을 InputVar 안에서 발견할 수 없었으면 1이 설정되고 그렇지 않으면 0이 설정됩니다.

논평

StringMid 그리고 InStr()와 다르게, StringGetPos에 대하여 0은 첫 번째 문자의 위치로 정의됩니다.

열람된 위치는 언제나 InputVar의 첫 문자에 상대적입니다. Occurrence 그리고/또는 Offset이 지정되어 있든 말든 상관이 없습니다. 예를 들어, 문자열 "abc"가 123abc789에서 발견되면, 보고된 그의 위치는 언제나 3입니다. 어떤 방법으로 발견했는지는 상관이 없습니다.

SearchText의 지정된 출현이 InputVar안에 존재하지 않으면, OutputVar는 -1이 설정되고 ErrorLevel는 1이 설정됩니다.

SplitPath를 사용하면 보다 쉽게 파일 경로를 그의 디렉토리와 파일이름 그리고 확장자로 가를 수 있습니다.

내장 변수 %A_Space%%A_Tab%에 각각 스페이스 문자 하나와 탭 문자 하나가 들어 있습니다. SearchText의 앞이나 뒤에서 스페이스와 탭을 따로 검색할 경우에 유용합니다.

InStr(), RegExMatch(), IfInString, if var in/contains MatchList, StringCaseSense, StringReplace, SplitPath, StringLeft, StringRight, StringMid, StringTrimLeft, StringTrimRight, StringLen, StringLower, StringUpper, if var is type

예제

Retrieves and analyzes the position of a substring.

Haystack := "abcdefghijklmnopqrs"
Needle := "def"
StringGetPos, pos, Haystack, %Needle%
if (pos >= 0)
    MsgBox, 문자열이 위치 %pos%에서 발견되었습니다.

파일이 전체 경로를 각 구성요소로 분해 합니다. Note that it would be much easier to use StrSplit(), StringSplit or a parsing loop to do this, so the below is just for illustration.

FileSelectFile, file, , , 깊숙히 내포된 폴더 안에서 파일이름 하나를 고르십시오:
if (file != "")
{
    pos_prev := StrLen(file)
    pos_prev += 1 ; 마지막 문자 뒤에 위치에 맞게 조절합니다.
    Loop
    {
        ; 오른쪽으로부터 N번째 출현을 검색합니다:
        StringGetPos, pos, file, \, R%A_Index%
        if ErrorLevel
            break
        length := pos_prev - pos - 1
        pos_prev := pos
        pos += 2  ; StringMid에 사용하기 위해 조정합니다.
        StringMid, path_component, file, %pos%, %length%
        MsgBox Path component #%A_Index% (from the right) is:`n%path_component%
    }
}