Please enable JavaScript to view this site.

thinBasic Help Manual

* * HELP & MANUAL PREMIUM PACK 4 DEMO VERSION * *

This documentation was styled with a demo skin from the Premium Pack 4 add-on for Help & Manual. The contents of the skin are encrypted and not configurable. You can only publish HM projects with this skin. You cannot edit it or change it.

This version is copyright and may only be used for local testing purposes. It may not be distributed.

Please purchase the full version of the Premium Pack to get the configurable skins and remove this notice. The package will also include the Toolbox configuration utility for Premium Pack skins.

 

Description

 

Search a string for the first occurrence of a specified character or string.

 

Syntax

 

n = INSTR([StartPos,] MainString, [ANY] MatchString [, nthOccurience ])

 

Returns

 

Number

 

Parameters

 

Name

Type

Optional

Meaning

StartPosition

Number

Yes

Search starting point inside MainString

MainString

String

No

String to search into

MatchString

String

No

String to search for

nthOccurience

Number

Yes

Search the nth position of MatchString

 

Remarks

 

If StartPosition is omitted, search will start from first byte of MainString going left to right.

If StartPosition is positive, search will go from left to right.

If StartPosition is negative, search will go from right to left.

 

If the ANY keyword is specified, MatchString specifies a list of single characters to be searched for individually.

 

Restrictions

 

INSTR is case-sensitive, meaning that upper-case and lower-case letters must match exactly in MatchString and MainString.

If MatchString is null, INSTR returns 1 (if StartPos not specified) or StartPos (if StartPos is specified).

 

See also

 

String Handling, EXTRACT$, LCASE$, LEFT$, LTRIM$, MID$, RIGHT$, RTRIM$, TALLY, TRIM$, UCASE$, VERIFY

 

Examples

 

Thanks to Abraxas for the following script example

' Usage of the INSTR Keyword example

 

Dim MainString  As String VALUE "My nice long string with some nice words that repeat themselves which is nice"

Dim MatchString As String VALUE "nice"

Dim sMsg        As String

Dim Startpos    As DWORD

Dim Pos         As DWORD

 

Pos = INSTR(StartPos, MainString, MatchString)

 

sMSG += "The word" & $CRLF & $CRLF 

sMsg += MatchString & $CRLF & $CRLF

sMsg += "Appears for the first time at position = " & Pos & $CRLF & $CRLF

 

MSGBOX 0, sMsg

 

 

'--------------------------------------------------------

'---Thanks to Abraxas for this example

'--------------------------------------------------------

' Usage of the INSTR Keyword example

 

Dim MainString  As String VALUE "My nice long string with some nice words that repeat themselves which is nice"

Dim MatchString As String VALUE "nice"

Dim sMsg        As String VALUE ""

Dim Pos         As Long   VALUE -1

Dim nTimes      As DWORD

 

While Pos <> 0

  Pos = INSTR(Pos + 1, MainString, MatchString)

  If Pos > 0 Then

    INCR nTimes

  End If

Wend

 

sMSG += "The number of times the word" & $CRLF & $CRLF 

sMsg += MatchString & $CRLF & $CRLF

sMsg += "Appears = " & nTimes & $CRLF & $CRLF

 

MSGBOX 0, sMsg

 

Created with Help+Manual 8 and styled with Premium Pack Version 4 © by EC Software