VBScriptのInStr関数
InStr関数は、別の文字列内の文字列の最初に出現する位置を返します。
InStr関数は次の値を返します。
- 文字列1 ""である場合 - InStr関数は0を返します。
- 文字列1がnullの場合 - InStr関数はヌルを返します。
- 文字列2 ""である場合 - InStr関数は、スタートを返します
- 文字列2がNULLの場合 - InStr関数はヌルを返します。
- 文字列2が見つからない場合 - InStr関数は0を返します。
- string2のは、中に文字列1の中に発見された場合 - InStr関数は一致する文字列の場所を見つけるに戻ります
- >レン(文字列1)起動した場合 - 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のリファレンス