Date: 02-16-2022
Return to Index
created by gbSnippets
'The PARSE statement is used to separate a string into an array, where
'each element is found between specified delimiters.
'Delimiters can be single characters, multi-character strings, or a list
'of delimiters.
'Primary Code:
'Syntax: PARSE main$, array$() [, {[ANY] delim$ | BINARY}]
'Example#1 - split on common delimiter
Dim MyArray() As String, a$
a$ = "1-2-3-4-5"
Parse a$, MyArray(), "-" 'splits string a$ at dash
'MyArray(0) = 1
'MyArray(1) = 2
'MyArray(2) = 3
'MyArray(3) = 4
'MyArray(4) = 5
'Example#2 - split on any one of several specified characters
Dim MyArray() As String, a$
a$ = "1:2,3,4:5"
Parse a$, r$, ANY ",:-" 'use of ANY splits a$ into array on comma, colon, or dash
'MyArray(0) = 1
'MyArray(1) = 2
'MyArray(2) = 3
'MyArray(3) = 4
'MyArray(4) = 5
'Example#3 - default split (on comma)
Dim MyArray() As String, a$
a$ = "1,2,3,4,5"
Parse a$, MyArray() 'splits string a$ at commas (default delimiter)
'MyArray(0) = 1
'MyArray(1) = 2
'MyArray(2) = 3
'MyArray(3) = 4
'MyArray(4) = 5
'Compilable Example: (Jose Includes)
'This example splits a string (numbers separated by commas), then uses JOIN$
'to combine them with $crlf and puts the result in a file. Notepad is used to open
'the result. Note the use of PARSECOUNT to determine how many elements PARSE
'will return.
#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
Local a$, ProcessID&
a$ = "1,2,3,4,5"
Dim MyArray(ParseCount(a$)-1) As String
Parse a$, MyArray() 'splits string a$ at commas (default delimiter)
Open "myfile.txt" For Output as #1
Print #1, Join$(MyArray(), $crlf)
Close #1
ProcessID& = Shell ("Notepad " + Exe.path$ + "myfile.txt", 1) 'opens Notepad, does not wait for it to close
End If
End Function
'gbs_00244
'Date: 03-10-2012
http://www.garybeene.com/sw/gbsnippets.htm