How can i use Regex.Split

bishmedia

Member
Licensed User
Longtime User
How can i get say the first bit of the text file into the listview eg AB15 without the quotes.???

I believe i use something called Regex.Split

B4X:
Sub Activity_Create(FirstTime As Boolean)
   'Do not forget to load the layout file created with the visual designer. For example:
   Activity.LoadLayout("csv_listview")
    Dim Reader As TextReader
       Reader.Initialize(File.OpenInput(File.DirAssets, "pc.csv"))
       Dim line As String
       line = Reader.ReadLine
      
      Do While line <> Null
          line = Reader.ReadLine         
          ListView1.AddSingleLine(line)
      Loop
      
End Sub
 

Attachments

  • listview.jpg
    listview.jpg
    61.7 KB · Views: 335

NJDude

Expert
Licensed User
Longtime User
Something like this:
B4X:
Dim Test As String
Dim Items() As String

Test = File.ReadString(File.DirAssets, "test.txt")

Items = Regex.Split(",", Test)
Msgbox(Items(0).Replace(QUOTE, ""), "")

The contents of "test.txt" is something like
B4X:
"AB15", "something", "more", "even more"
 
Upvote 0

bishmedia

Member
Licensed User
Longtime User
i keep getting an error on yours and my try....


Parsing code. Error
Error parsing program.
Error description: Syntax error.
Occurred on line: 35
35 ListView1.AddSingleLine(Items(0).Replace(QUOTE,*""),*"")

B4X:
      Do While line <> Null
          Dim Items() As String
         line = Reader.ReadLine
          Items = Regex.Split(",", line)         
          ListView1.AddSingleLine(Items(0).Replace(QUOTE,*""),*"")         
      Loop
 
Upvote 0

bishmedia

Member
Licensed User
Longtime User
Many thanks, the listview populates great but then comes up with an error as follows...

java.lang.NullPointerException

If i press yes to continue it runs fine, how can i stop this error, my code now is...

B4X:
   Do While line <> Null
          Dim Items() As String
         line = Reader.ReadLine
          Items = Regex.Split(",", line)   
         Items(0) = Items(0).Replace(QUOTE, "")
         ListView1.AddSingleLine(Items(0))          
      Loop
 
Upvote 0

agraham

Expert
Licensed User
Longtime User
I assume you must have set line before entering the loop in which case ReadLine is in the wrong place and you are ignoring the first line and processing the null at the end which is giving the exception.

B4X:
line = Reader.ReadLine
Do While line <> Null
    Dim Items() As String
    Items = Regex.Split(",", line)    
    Items(0) = Items(0).Replace(QUOTE, "")
    ListView1.AddSingleLine(Items(0))
    line = Reader.ReadLine           
Loop
 
Upvote 0
Top