VBScript InStr関数
完全版 VBScript リファレンスマニュアル
InStr 関数は、ある文字列が別の文字列の中で最初に見つかる位置を返します。
InStr 関数は次の値を返します:
- string1 が "" の場合 - InStr は 0 を返します
- string1 が Null の場合 - InStr は Null を返します
- string2 が "" の場合 - InStr は start を返します
- string2 が Null の場合 - InStr は Null を返します
- string2 が見つからない場合 - InStr は 0 を返します
- string1 内で string2 が見つかった場合 - InStr は一致した文字列の位置を返します
- start > Len(string1) の場合 - InStr は 0 を返します
ヒント:InStrRev 関数を参照してください。
構文
InStr([start,]string1,string2[,compare])
| 引数 | 説明 |
|---|---|
| start | 省略可能です。各検索の開始位置を指定します。既定の検索開始位置は最初の文字(1)です。compare 引数を指定した場合は、この引数も指定する必要があります。 |
| string1 | 必須です。検索対象となる文字列です。 |
| string2 | 必須です。検索する文字列式です。 |
| compare | 省略可能です。使用する文字列比較の種類を指定します。既定値は 0 です。 次の値を指定できます:
|
例
例 1
<script type="text/vbscript">
txt="This is a beautiful day!"
document.write(InStr(txt,"beautiful"))
</script>
txt="This is a beautiful day!"
document.write(InStr(txt,"beautiful"))
</script>
上記の例の出力結果:
11
試してみる »
例 2
文字 "i" を異なる開始位置で検索します:
<script type="text/vbscript">
txt="This is a beautiful day!"
document.write(InStr(1,txt,"i") & "<br />")
document.write(InStr(7,txt,"i") & "<br />")
</script>
txt="This is a beautiful day!"
document.write(InStr(1,txt,"i") & "<br />")
document.write(InStr(7,txt,"i") & "<br />")
</script>
上記の例の出力結果:
3
16
16
試してみる »
例 3
文字 "t" をテキスト比較とバイナリ比較で検索します:
<script type="text/vbscript">
txt="This is a beautiful day!"
document.write(InStr(1,txt,"t",1) & "<br />")
document.write(InStr(1,txt,"t",0) & "<br />")
</script>
txt="This is a beautiful day!"
document.write(InStr(1,txt,"t",1) & "<br />")
document.write(InStr(1,txt,"t",0) & "<br />")
</script>
上記の例の出力結果:
1
15
15
試してみる »
完全版 VBScript リファレンスマニュアル その他の拡張