Android Question retrieve values between commas in different fields

fgh3966

Active Member
Licensed User
I am getting rows from CSV file.
These lines contain commas which separate the text fields and i would like each field to be copied into a string dedicated to it.
I looked with regex.split but couldn't find any help for this function.
How could we do it ?

example this row : aaa,192,xyz,168

to string(1) aaa
string(2) 192
string(3) xyz
string(4) 168

Thanks
 
ChatGPT says...

B4X: Copy
' Assuming 'line' is your CSV string: "aaa,192,xyz,168"
Dim line As String = "aaa,192,xyz,168"
Dim fields() As String
fields = Regex.Split(",", line)

' Now fields() array will contain the values
For i = 0 To fields.Length - 1
    Log($"String(${i + 1}) ${fields(i)}"$)
Next
 
Upvote 0
perhaps you'll be lucky, but many csv files contain "fields" that contain commas (and usually offset within quotation marks). splitting lines on commas won't work. at least not as expected. a robust csv parser is needed. i think there several found on the forum. and certainly in the wild.
 
Upvote 0
Back
Top