get items, regex

Cor

Active Member
Licensed User
Longtime User
item1,item2,item3

how to get item1,item2,item3 with regex

item=regex.split(",")


grCor
 

derez

Expert
Licensed User
Longtime User
Dim str() as string

str = regex.split(",", somestring)
item1 = str(0)
item2 = str(1)
...
 
Upvote 0

Cor

Active Member
Licensed User
Longtime User
line is one of the following
Aarle-rixtel,1
Alphen nb,1

B4X:
dim splitLine() as string

splitLine=Regex.Split(",",line)

gemeente=splitLine(0)
clubs=splitLine(1)  ' should show 1 , but shows name

' shows both the first item
 
Upvote 0

Cor

Active Member
Licensed User
Longtime User
seems that if we want 2 second item we need an extra comma at the end

but how to get without extra comma?
 
Upvote 0

Cor

Active Member
Licensed User
Longtime User
yes, i have tried

when i put an extra comma at the end then i get the second item

item1,item2

splitString(0) and splitString(1) returns item1

when using extra comma at the end then
splitString(0) returns item1 and splitString(1) returns item2

--- it works if you convert it to a list---

Dim l As List
l.Initialize2(numbers)
 
Upvote 0

agraham

Expert
Licensed User
Longtime User
As Erel said, there is no need for a comma at the end. This works as expected for me.

B4X:
Dim splitLine() As String
line = "Aarle-rixtel,1"
splitLine=Regex.Split(",",line)
gemeente=splitLine(0) ' returns "Aarle-rixtel"
clubs=splitLine(1) ' returns 1 as expected 
Msgbox(clubs, gemeente)
 
Upvote 0

Cor

Active Member
Licensed User
Longtime User
not if you read items from a text file

below works if you use sList as list, not working if using string

You are right if you use:
B4X:
splitLine=Regex.Split(",",line)
gemeente=splitLine(0) ' returns "Aarle-rixtel"
clubs=splitLine(1) ' returns 1 as expected

B4X:
clubList=File.readlist(File.DirAssets,"brabant.txt")
For cnt = 0 To clubList.Size-1
  line=clubList.Get(cnt) '  splitLine(0), splitLine(1) does not work if reading from list

  ' line="item1,2,3"  ' this works

If line.StartsWith(" ")=False Then
     splitLine=Regex.Split(",",line)
    sList.Initialize2(splitLine)
    gemeente=sList.Get(0)
    clubs=sList.Get(1)
    listViewGemeente.Addtwolines2(gemeente,"Aantal clubs: " & clubs,cnt)  ' set index of gemeente
  End If
 
Upvote 0

Cor

Active Member
Licensed User
Longtime User
Problem solved,

My editor editplus saved it as standard ANSI

Changed to UTF8 and it worked.

So when using text files with android I think always save it as UTF8

notepad saves it also as default to ANSI, so this will also fails
 
Upvote 0
Top