InStr()

왼쪽이나 오른쪽으로부터 주어진 문자열이 나타나는지 검색합니다.

FoundPos := InStr(Haystack, Needle , CaseSensitive := false, StartingPos := 1, Occurrence := 1)

매개변수

Haystack

내용을 검색할 문자열.

Needle

검색할 문자열.

CaseSensitive

매개변수 CaseSensitive를 생략하거나 거짓이면, 대소문자를 구분하지 않고 검색합니다 (대소문자의 구별은 StringCaseSense에 따릅니다); 그렇지 않으면, 대소문자가 정확하게 일치해야 합니다.

StartingPos

StartingPos를 생략하면, 1이 기본값이 됩니다 (Haystack의 시작). 그렇지 않으면, 2를 지정해 두 번째 문자에서 시작하거나 3을 지정해 세 번째 문자에서 시작할 수 있습니다. 등등.

StartingPosHaystack의 길이를 넘어서면, 0이 반환됩니다. [AHK_L 57+]: StartingPos가 0이거나 음수이면, 끝에서부터 해당 오프셋에서 시작하여 역방향으로 (오른쪽에서 왼쪽으로) 검색이 수행됩니다.

StartingPos의 값에 상관없이, 반환 값은 언제나 Haystack의 첫 문자에 상대적입니다. 예를 들어, "123abc789"에서 "abc" 의 위치는 언제나 4입니다.

Occurrence [AHK_L 57+]

If Occurrence is omitted, it defaults to the first match of the Needle in Haystack. Occurrence에 2을 지정하면 두 번째 부합한 위치를 돌려주고, 3은 세 번째 부합 등등의 위치를 돌려줍니다.

반환 값

This function returns the position of an occurrence of the string Needle in the string Haystack. 위치 1이 첫 번째 문자입니다; 왜냐하면 0은 "false"와 동의어이기 때문에, 직관적으로 "발견하지 못함"의 뜻이 됩니다.

An occurrence of an empty string ("") can be found at any position; therefore, if Needle is an empty string, the return value is 1. As a blank Needle would typically only be passed by mistake, it will be treated as an error in AutoHotkey v2.

논평

This function is a combination of IfInString and StringGetPos.

RegExMatch() can be used to search for a pattern (regular expression) within a string, making it much more flexible than InStr(). However, InStr() is generally faster than RegExMatch() when searching for a simple substring.

RegExMatch(), StringGetPos, IfInString, StringCaseSense, if var in/contains MatchList, if var between, if var is type

예제

Reports the 1-based position of the substring "abc" in the string "123abc789".

MsgBox % InStr("123abc789", "abc") ; Returns 4

Searches for Needle in Haystack.

Haystack := "The Quick Brown Fox Jumps Over the Lazy Dog"
Needle := "Fox"
If InStr(Haystack, Needle)
    MsgBox, 문자열이 발견되었습니다.
Else
    MsgBox, The string was not found.

Demonstrates the difference between a case insensitive and case sensitive search.

Haystack := "The Quick Brown Fox Jumps Over the Lazy Dog"
Needle := "the"
MsgBox % InStr(Haystack, Needle, false, 1, 2) ; case insensitive search, return start position of second occurence
MsgBox % InStr(Haystack, Needle, true) ; case sensitive search, return start position of first occurence, same result as above