文字列の左または右から、指定された出現箇所を検索します。
FoundPos := InStr(Haystack, Needle , CaseSense, StartingPos, Occurrence)
型:文字列
内容を検索する文字列。
型:文字列
検索する文字列です。
型:String or Integer (boolean)
If omitted, it defaults to Off. それ以外のときは、次のいずれかの値を指定します:
On または 1 (true):検索では大文字と小文字が区別される。
Off または 0 (false):The search is not case-sensitive, i.e. the letters A-Z are considered identical to their lowercase counterparts.
Locale: The search is not case-sensitive according to the rules of the current user's locale. 例えば、英語や西欧のロケールでは、A~Zの小文字だけでなく、ÄやÜなどの非ASCII文字も小文字と同じように扱われることがほとんどです。Localeは、比較する文字列の性質によって、Offの1~8倍遅くなります。
型:整数
If omitted, the entire string is searched. それ以外の場合は、検索を開始する位置を指定します。1が最初の文字、2が2番目の文字、といった具合に指定します。Negative values count from the end of Haystack, so -1 is the last character, -2 is the second-last, and so on.
Occurrenceが省略された場合、StartingPosが負であれば、右から左へ検索が行われる。ただし、Occurrenceを指定した場合、StartingPosは検索の方向には影響しません。
For a right-to-left search, StartingPos specifies the position of the last character of the first potential occurence of Needle. 例えば、InStr("abc", "bc",, 2, +1)
はマッチを見つけるが、InStr("abc", "bc",, 2, -1)
はそうではありません。
StartingPosの絶対値がHaystackの長さより大きい場合、0 が返されます。
型:整数
If omitted, it defaults to the first match in Haystack. The search is conducted from right to left if StartingPos is negative; otherwise it is conducted from left to right.
Occurrenceが正の場合、検索は常に左から右へ行われる。Occurrenceに2を指定すると、2番目にマッチした位置を、3を指定すると3番目にマッチした位置を返します。
Occurrenceが負の場合、検索は常に右から左へ行われます。例えば、-2は右から2番目に出現するものを検索します。
型:整数
この関数は、文字列Haystackの中で文字列Needleが出現する位置を返します。これは、0が「偽」と同義であり、直感的に「見つからない」ことを示すからである。
StartingPosやOccurrenceの値に関係なく、戻り値は常にHaystackの最初の文字に対する相対値です。例えば、"123abc789"の "abc"の位置は、常に4です。
従来、空文字列(""
)は、どの位置でも出現することができました。However, as a blank Needle would typically only be passed by mistake, it is treated as an error (an exception is thrown).
以下のいずれかに該当する場合、ValueErrorが発生します:
RegExMatchは、文字列の中にあるパターン(正規表現)を検索することができるので、InStrよりはるかに柔軟性があります。しかし、単純な部分文字列を検索する場合は、一般にInStrの方がRegExMatchより高速です。
InStrは最初のバイナリゼロ(ヌルターミネーター)までしか検索しませんが、RegExMatchはバイナリゼロが含まれていても、文字列の全長を検索しています。
Searches for Needle in Haystack.
Haystack := "The Quick Brown Fox Jumps Over the Lazy Dog" Needle := "Fox" If InStr(Haystack, Needle) MsgBox "The string was found." 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