Date: 02-16-2022
Return to Index
created by gbSnippets
'Programmers often merge string information into a single string, separated
'by a known delimiter. It simplifies reading and writing the information to
'a file, or making copies of the data - at the expense of having to convert
'the data. The PowerBASIC PARSE$ function is used to separate the string
'into the component strings.
'Primary Code:
'Syntax: a$ = Parse$(string_expr [, {[ANY] string_delimiter | BINARY}], index&)
'Here are 3 ways in which the syntax can be applied:
Dim a$, return$
a$ = "123:::456:::789" 'example of 3 strings separated by ":::" delimiter
return$ = Parse$(a$, ":::", 1) 'returns 123
a$ = "123:456,789" 'note the different delimiters in the string
return$ = Parse$(a$, ANY ":,';", 2) 'returns 123 'ANY means any one delimiter
a$ = $dq + "123" + $dq + "," + $dq + "456" + $dq + "," + $dq + "789" + $dq 'CSV string
return$ = Parse$(a$, 3) 'returns 789
'The Parse$ function can extract just a single delimited string from
'a larger string, without first having to Parse the string into an array
'Default delimiter is a comma
'Delimiters are not returned as part of the returned strings
'Delimiters are case-sensitive
'Fields start at 1 (not zero)
'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 a$, iReturn$
a$ = "123:::456:::789" 'example of 3 strings separated by ":::" delimiter
iReturn$ = Parse$(a$, ":::", 1) 'returns 123
MsgBox iReturn$
a$ = "123:456,789" 'note the different delimiters in the string
iReturn$ = Parse$(a$, ANY ":,';", 2) 'returns 123 'ANY means any one delimiter
MsgBox iReturn$
a$ = $dq + "123" + $dq + "," + $dq + "456" + $dq + "," + $dq + "789" + $dq 'CSV string
iReturn$ = Parse$(a$, 3) 'returns 789
MsgBox iReturn$
End If
End Function
'gbs_00238
'Date: 03-10-2012
http://www.garybeene.com/sw/gbsnippets.htm