20. juli 2001 - 22:32
Der er
3 kommentarer og
1 løsning
Parsing
Okay, jeg har en variabel, sData, der indeholder:
HEJ MED DIG
Så vil jeg have en funktion der deler det op i f.eks.:
T(1)=\"HEJ\"
T(2)=\"MED\"
T(3)=\"DIG\"
Altså ligesom Split() i PHP / C
Jo du svarede godt nok, men inden du svarede havde jeg fundet en funktion som jeg brugte, nemlig:
Public Function ArgSplit%(CLine1$, Arguement$())
\'CLine1$ is the string that needs to be
\' split
\'TheArray$() is the array to hold the sp
\' lit arguments
\'TheArray$() must be predimentioned to b
\' e large enough to hold the number
\'of possible arguments
ArgSplit% = ArgSplitEX%(CLine1$, Arguement$, \" \")
End Function
Public Function ArgSplitEX%(CLine1$, Argument$(), Seperator$)
\'CLine1$ is the string that needs to be
\' split
\'Argument$() is the array to hold the sp
\' lit arguments
\'Seperator$ is the seperation character
\'Argument$() must be predimentioned to b
\' e large enough to hold the number
\'of possible arguments
Dim cline$, Length%, Arg1%, I%, InWord%, InQuotes%
\'ReDim Arg$(1) \'Make sure we have at lea
\' st 1 element!
cline$ = RTrim$(LTrim$(CLine1$))
Length% = Len(cline$)
If Length% <= 0 Then ArgSplitEX% = -1: Exit Function
I% = 1: Arg1% = LBound(Argument$): InWord = -1
Argument$(Arg1%) = \"\"
While I% <= Length%
Ch$ = Mid$(cline$, I%, 1)
If Ch$ <> Seperator$ Then \'it is NOT a Seperator
If Ch$ = Chr$(34) Then
InQuotes% = Not InQuotes%
\'InWord% = InQuotes
Else
If Not InWord% Then InWord% = -1
Argument$(Arg1%) = Argument$(Arg1%) + Ch$
End If
Else \'it IS a Seperator
If InQuotes Then
Argument$(Arg1%) = Argument$(Arg1%) + Ch$
Else
If InWord Then
Arg1% = Arg1% + 1
\'ReDim Preserve Arg$(Arg1%)
InWord% = 0
Argument$(Arg1%) = \"\"
End If
End If
End If
I% = I% + 1
Wend
ArgSplitEX% = (Arg1% + 1) - LBound(Argument$)
End Function
Sub Command1_Click()
Dim Arguments$(1 To 100)
Call ArgSplitEX%(Command$, Arguments$(), \" \")
End Sub