Funkce VBScript InStr


❮ Kompletní VBScript Reference

Funkce InStr vrací pozici prvního výskytu jednoho řetězce v jiném.

Funkce InStr může vrátit následující hodnoty:

  • Pokud je řetězec1 "" - InStr vrátí 0
  • Pokud má řetězec1 hodnotu Null - InStr vrátí hodnotu Null
  • Pokud je řetězec2 "" - InStr vrátí start
  • Pokud má řetězec2 hodnotu Null - InStr vrátí hodnotu Null
  • Pokud řetězec2 není nalezen - InStr vrátí 0
  • Pokud je řetězec2 nalezen v řetězci1 - InStr vrátí pozici, na které byla nalezena shoda
  • Pokud start > Len(řetězec1) - InStr vrátí 0

Tip: Podívejte se také na funkci InStrRev

Syntax

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

Příklady

Příklad 1

<%

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

%>

Výstupem výše uvedeného kódu bude:

11

Příklad 2

Nalezení písmene "i" pomocí různých výchozích pozic:

<%

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

%>

Výstupem výše uvedeného kódu bude:

3
16

Příklad 3

Nalezení písmene "t" s textovým a binárním srovnáním:

<%

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

%>

Výstupem výše uvedeného kódu bude:

1
15

❮ Kompletní VBScript Reference