VBspeed / String / InStr
VBspeed © 2000-10, updated: 05-Oct-2002
InStr
See also InStrRev


The Definition
Function InStr
Returns the position of the first occurrence of one string within another.
Native to VB5 and VB6 (actually to all versions of VB, even to all versions of BASIC, AFAIK).
VBspeed's Declaration:
InStr(sCheck, sMatch[, Start[, Compare]])
VB's native Declaration:
VB's InStr, of course, looks like this (with the optional Start parameter as the first argument, a weird setup if you ask me):
InStr([Start, ]sCheck, sMatch[, Compare])
Arguments:
sCheckRequired. String expression being searched.
sMatchRequired. String expression being searched for.
StartOptional. Numeric expression that sets the starting position for each search. If omitted, search begins at the first character position (Start = 1).
CompareOptional. Numeric value indicating the kind of comparison to use when evaluating substrings. If omitted, a binary comparison is performed.
Return Values:
Typical cases:
InStr("abc", "a")             =>  1
InStr("abc", "b")             =>  2

Special cases:
sCheck is zero-length         =>  0
sMatch is zero-length         =>  start
sMatch is not found           =>  0
Start > Len(sMatch)           =>  0
Roll your own
If you want to have a go at InStr yourself, use this function (VB5/6-compatible) to verify the correctness of your code.


The Charts
Calls
  lRet = InStr(sCheck, sMatch, , Compare)
Call 1 sCheck = "http://www.xbeat.net/vbspeed/index.htm"
sMatch = ":"
Call 2 sCheck = "http://www.xbeat.net/vbspeed/index.htm"
sMatch = "."
Call 3 sCheck = "http://www.xbeat.net/vbspeed/index.htm"
sMatch = "m"
Call 4 sCheck = "http://www.xbeat.net/vbspeed/index.htm"
sMatch = "M"
Compare = vbTextCompare
Call 5 sCheck = "http://www.xbeat.net/vbspeed/index.htm"
sMatch = "www"
Call 6 sCheck = "http://www.xbeat.net/vbspeed/index.htm"
sMatch = "WWW"
Compare = vbTextCompare
Call 7 sCheck = Space$(999) & String$(99, "x") & Space(99)
sMatch = "x"  [many occurrences, but very far from left end]
Call 8 sCheck = Space$(100)
sMatch = "x"  [no occurrences]
 VB5
CodeAuthorDopingNotes
InStr VB5/6  
InStr01 MarzoAPI 
Call 1
21.080.070µs
11.000.065µs
Call 2
11.000.076µs
21.250.094µs
Call 3
11.000.094µs
21.360.128µs
Call 4
28.071.311µs
11.000.162µs
Call 5
11.000.080µs
21.210.097µs
Call 6
211.061.328µs
11.000.120µs
Call 7
11.001.415µs
22.303.256µs
Call 8
11.000.182µs
21.990.361µs
 VB6
CodeAuthorDopingNotes
InStr VB5/6  
InStr01 MarzoAPI 
Call 1
11.000.063µs
21.020.064µs
Call 2
11.000.072µs
21.280.092µs
Call 3
11.000.087µs
21.400.122µs
Call 4
28.221.306µs
11.000.159µs
Call 5
11.000.076µs
21.190.090µs
Call 6
210.671.309µs
11.000.123µs
Call 7
11.001.414µs
22.173.065µs
Call 8
11.000.183µs
21.760.322µs
Conclusions
It's not easy to defeat native InStr, but there's good news for people that have to use InStr in TextCompare mode.
Mail your code! How to read all those numbers


The Code
InStr01
submitted 05-Oct-2002 by Marzo Junior  
Doping: TLB (cf. Dope'n'Declarations)
Class-wrapped. The class, which also includes a bunch of related functions, is waiting for you here.
Author's comments:
Donald's comments:

top | charts




VBspeed © 2000-10 by Donald Lessau