Função InStr do VBScript
❮ Referência completa do VBScript
A função InStr retorna a posição da primeira ocorrência de uma string dentro de outra.
A função InStr pode retornar os seguintes valores:
- Se string1 for "" - InStr retornará 0
- Se string1 for Null - InStr retornará Null
- Se string2 for "" - InStr retorna start
- Se string2 for Null - InStr retornará Null
- Se string2 não for encontrado - InStr retorna 0
- Se a string2 for encontrada dentro da string1 - InStr retorna a posição na qual a correspondência foi encontrada
- Se iniciar > Len(string1) - InStr retorna 0
Dica: Veja também a função InStrRev
Sintaxe
InStr([start,]string1,string2[,compare])
Parameter | Description |
---|---|
start | Optional. Specifies the starting position for each search. The search begins at the first character position (1) by default. This parameter is required if compare is specified |
string1 | Required. The string to be searched |
string2 | Required. The string expression to search for |
compare | Optional. Specifies the string comparison to use.
Default is 0 Can have one of the following values:
|
Exemplos
Exemplo 1
<%
txt="This is a beautiful day!"
response.write(InStr(txt,"beautiful"))
%>
A saída do código acima será:
11
Exemplo 2
Encontrando a letra "i", usando diferentes posições iniciais:
<%
txt="This is a beautiful day!"
response.write(InStr(1,txt,"i") & "<br />")
response.write(InStr(7,txt,"i") & "<br />")
%>
A saída do código acima será:
3
16
Exemplo 3
Encontrando a letra "t", com comparação textual e binária:
<%
txt="This is a beautiful day!"
response.write(InStr(1,txt,"t",1) & "<br />")
response.write(InStr(1,txt,"t",0) & "<br />")
%>
A saída do código acima será:
1
15
❮ Referência completa do VBScript