Fonction VBScript InStr


❮ Référence complète de VBScript

La fonction InStr renvoie la position de la première occurrence d'une chaîne dans une autre.

La fonction InStr peut renvoyer les valeurs suivantes :

  • Si string1 est "" - InStr renvoie 0
  • Si string1 est Null - InStr renvoie Null
  • Si string2 est "" - InStr renvoie start
  • Si string2 est Null - InStr renvoie Null
  • Si string2 n'est pas trouvé - InStr renvoie 0
  • Si string2 est trouvé dans string1 - InStr renvoie la position à laquelle la correspondance est trouvée
  • Si début > Len(string1) - InStr renvoie 0

Astuce : Regardez aussi la fonction InStrRev

Syntaxe

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:

  • 0 = vbBinaryCompare - Perform a binary comparison
  • 1 = vbTextCompare - Perform a textual comparison

Exemples

Exemple 1

<%

txt="This is a beautiful day!"
response.write(InStr(txt,"beautiful"))

%>

La sortie du code ci-dessus sera :

11

Exemple 2

Trouver la lettre "i", en utilisant différentes positions de départ :

<%

txt="This is a beautiful day!"
response.write(InStr(1,txt,"i") & "<br />")
response.write(InStr(7,txt,"i") & "<br />")

%>

La sortie du code ci-dessus sera :

3
16

Exemple 3

Trouver la lettre "t", avec comparaison textuelle et binaire :

<%

txt="This is a beautiful day!"
response.write(InStr(1,txt,"t",1) & "<br />")
response.write(InStr(1,txt,"t",0) & "<br />")

%>

La sortie du code ci-dessus sera :

1
15

❮ Référence complète de VBScript