Função VBScript InStr
função InStr retorna a posição da primeira ocorrência de uma string em outra string.
função InStr retorna os seguintes valores:
- Se string1 é "" - InStr retorna 0
- Se string1 é nulo - InStr retorna Null
- Se string2 é "" - InStr retorna início
- Se string2 é nulo - InStr retorna Null
- Se string2 não for encontrado - InStr retorna 0
- Se string2 é encontrado dentro de string1 in - InStr retorna para encontrar o local cadeia correspondente
- Se start> Len (string1) - InStr retorna 0
Dica: Veja a função InStrRev.
gramática
InStr([start,]string1,string2[,compare])
参数 | 描述 |
---|---|
start | 可选。规定每次搜索的起始位置。默认的搜索起始位置是第一个字符(1)。如果已规定 compare 参数,则必须有此参数。 |
string1 | 必需。需要被搜索的字符串。 |
string2 | 必需。需要搜索的字符串表达式。 |
compare | 可选。规定要使用的字符串比较类型。默认是 0。 可采用下列的值:
|
Exemplos
exemplo 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>
Exemplos de saída acima:
11
tente »
exemplo 2
Encontrar a letra "i", com diferentes posição inicial:
<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>
Exemplos de saída acima:
3
16
16
tente »
exemplo 3
Encontrar a letra "t", usando texto e comparação binária:
<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>
Exemplos de saída acima:
1
15
15
tente »
Completa VBScript Referência