VBScript InStr Fonction
fonction InStr renvoie la position de la première occurrence d'une chaîne dans une autre chaîne.
fonction InStr renvoie les valeurs suivantes:
- Si string1 est "" - InStr renvoie 0
- Si string1 est Null - InStr renvoie Null
- Si chaîne2 est "" - InStr renvoie début
- Si chaîne2 est Null - InStr renvoie Null
- Si chaîne2 est introuvable - InStr renvoie 0
- Si chaîne2 se trouve au sein de string1 in - InStr retourne pour trouver l'emplacement string correspondant
- Si le démarrage> Len (string1) - InStr renvoie 0
Astuce: Voir la fonction InStrRev.
grammaire
InStr([start,]string1,string2[,compare])
参数 | 描述 |
---|---|
start | 可选。规定每次搜索的起始位置。默认的搜索起始位置是第一个字符(1)。如果已规定 compare 参数,则必须有此参数。 |
string1 | 必需。需要被搜索的字符串。 |
string2 | 必需。需要搜索的字符串表达式。 |
compare | 可选。规定要使用的字符串比较类型。默认是 0。 可采用下列的值:
|
Exemples
exemple 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>
Des exemples de sortie ci-dessus:
11
Essayez »
exemple 2
Trouver la lettre "i", avec la position de départ différente:
<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>
Des exemples de sortie ci-dessus:
3
16
16
Essayez »
exemple 3
Trouver la lettre "t", utilisant à la fois du texte et comparaison binaire:
<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>
Des exemples de sortie ci-dessus:
1
15
15
Essayez »
Complete VBScript Référence