Android Question parsing string

Addo

Well-Known Member
Licensed User
Longtime User
i have the following string

msg~name~Text~

i am trying to parse them into parameters

the separator should be ~

so the result need

param[1] = msg

param[2] = name

param[3]
= text

and so on i have been doing this in delphi by doing following

B4X:
DECODES := 'msg~name~Text~';

ParamsCount := 0;
while (DECODES <> '') do
begin
Inc(ParamsCount);
P := Pos('~', DECODES);
if P = 0 then
Params[ParamsCount] := DECODES
else
begin
Params[ParamsCount] := Copy(DECODES, 1, P - 1);
Delete(DECODES, 1, P);
end;
end;

i have seen parsing example in the forum but here what i have tried

B4X:
Dim param As String = msg
Dim paramnum () As String = Regex.Split("\~", param)
For Each n As String In paramnum
Log(n)
Next

how to get N[1] N[2] N[3] ?

is it safe and correct to use Paramnum(0) and so on ?
 

DonManfred

Expert
Licensed User
Longtime User
B4X:
    Dim s As String = "msg~name~Text~"
    Dim paramnum () As String = Regex.Split("\~", s)
    For Each n As String In paramnum
        Log(n)
    Next
prints
B4X:
msg
name
Text
for me....
You also can use the index if you check the max index beforehand...
 
Upvote 0
Top