Date: 02-16-2022
Return to Index
created by gbSnippets
'INSTR is the primary PowerBASIC function for locating one string within another
'MID$ is also useful - return a portion of a string specified by position and length
'INSTR--------------------------------------------------------------
'Syntax: y& = INSTR([n&,] MainString, [ANY] MatchString) 'case-sensitive
'Examples:
i% = Instr("abcdefgcd", "cd") 'returns 3 - starting position optional
i% = Instr(1, "abcdefgcd", "cd") 'returns 3 - search for "cd" begins at position 1
i% = Instr(4, "abcdefgcd", "cd") 'returns 8 - search for "cd" begins at position 4
i% = Instr(-1, "abcdefgcd", "cd") 'returns 8 - search for "cd" begins at position -1 (far right)
'INSTR also supports a special ANY argument, which treats the MatchString as a list
'of single characters. INSTR then returns first match of any one of the MatchString characters
i% = Instr(-1, "abcdefgcd", ANY "cf") 'returns 3 - first matching character from "cf"
'Consider these other string functions which give information about 1 string within another
'MID - returns/replaces a substring from a main string
'TALLY - how many occurrences of characters or strings within another string
'VERIFY - whether each character of a string is present in another string
'Compilable Example: (Jose Includes)
#Compiler PBWin 9, PBWin 10
#Compile EXE
#Dim All
%Unicode=1
#Include "Win32API.inc"
Global hDlg As DWord
Function PBMain() As Long
Dialog New Pixels, 0, "Test Code",300,300,200,200, %WS_OverlappedWindow To hDlg
Control Add Button, hDlg, 100,"Push", 50,10,100,20
Dialog Show Modal hDlg Call DlgProc
End Function
CallBack Function DlgProc() As Long
If CB.Msg = %WM_Command AND CB.Ctl = 100 AND CB.Ctlmsg = %BN_Clicked Then
Dim i%
i% = Instr("abcdefgcd", "cd") 'returns 3 - starting position optional
MsgBox Str$(i)
i% = Instr(1, "abcdefgcd", "cd") 'returns 3 - search for "cd" begins at position 1
MsgBox Str$(i)
i% = Instr(4, "abcdefgcd", "cd") 'returns 8 - search for "cd" begins at position 4
MsgBox Str$(i)
i% = Instr(-1, "abcdefgcd", "cd") 'returns 8 - search for "cd" begins at position -1 (far right)
MsgBox Str$(i)
i% = Instr(-1, "abcdefgcd", ANY "cf") 'returns 8 - first matching character (c or f) starting from right
MsgBox Str$(i)
End If
End Function
'gbs_00240
'Date: 03-10-2012
http://www.garybeene.com/sw/gbsnippets.htm