B4J Question Split

sdleidel

Active Member
Licensed User
Longtime User
hi...
I have different Output from my Database...
Sample:
12,23,,33,,,,1
14,,,3,18,,,
....
I Save the result as Array... and write the Array in a tableview...

When i use the regex.split,
the last fields are cut (18,,, ) and the Array is to small...
how i can fix it ?

I think i must write my own Split Routine
 

ilan

Expert
Licensed User
Longtime User
yes you are right there is an issue:

B4X:
    Dim txt() As String = Array As String("12,23,,33,,,,1", "14,,,3,18,,,")
  
    For i = 0 To txt.Length-1
        Dim str() As String = Regex.Split(",",txt(i))
        For j = 0 To str.Length-1
            If str(j).Length = 0 Then
                Log("empty string")
            Else
                Log(str(j)) 
            End If
        Next
    Next

log:

Waiting for debugger to connect...
Program started.
12
23
empty string
33
empty string
empty string
empty string
1
14
empty string
empty string
3
18

maybe add a value at the end without counting it, something like this:

B4X:
    Dim txt() As String = Array As String("12,23,,33,,,,1", "14,,,3,18,,,")
 
    For i = 0 To txt.Length-1
        txt(i) = txt(i) & ",end" 'update array line
        Dim str() As String = Regex.Split(",",txt(i))
        For j = 0 To str.Length -2 ' dont count the last value
            If str(j).Length = 0 Then
                Log("empty string")
            Else
                Log(str(j))   
            End If
        Next
    Next

Waiting for debugger to connect...
Program started.
12
23
empty string
33
empty string
empty string
empty string
1
14
empty string
empty string
3
18
empty string
empty string
empty string
 
Upvote 0

sdleidel

Active Member
Licensed User
Longtime User
Hi Erel,
yes i have read the documentation.
A Option at the Split, that we can turn off the remove of emty matches would great...
 
Upvote 0

emexes

Expert
Licensed User
This works for your demo data, returns only numbers, no blank entries?
B4X:
Dim Lines() As String = Array As String("12,23,,33,,,,1", "14,,,3,18,,,")

For Each L As String In Lines
    Dim Numbers() As String = Regex.Split(",+", L)    'or Regex.Split("\D+", L)
    For Each N As String In Numbers
        Log("[" & N & "]")    'enclose in square brackets to ensure any spurious blank spaces or lines are exposed
    Next
Next
B4X:
Program started.
[12]
[23]
[33]
[1]
[14]
[3]
[18]
 
Last edited:
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
He wants the empty elements to be kept.

B4X:
Sub AppStart (Form1 As Form, Args() As String)
   Dim txt() As String = Array As String("12,23,,33,,,,1", "14,,,3,18,,,")
   For Each s As String In txt
       Dim split() As String = SplitWithAllElements(s)
       For Each n As String In split
           Log("n: " & n)
       Next
   Next
End Sub

Sub SplitWithAllElements(txt As String) As String()
   txt = txt & ",~"
   Dim s() As String = Regex.Split(",", txt)
   Dim s2(s.Length - 1) As String
   Bit.ArrayCopy(s, 0, s2, 0, s2.Length)
   Return s2
End Sub
 
Upvote 0

sdleidel

Active Member
Licensed User
Longtime User
Perfect (-:
Thank you.



He wants the empty elements to be kept.

B4X:
Sub AppStart (Form1 As Form, Args() As String)
   Dim txt() As String = Array As String("12,23,,33,,,,1", "14,,,3,18,,,")
   For Each s As String In txt
       Dim split() As String = SplitWithAllElements(s)
       For Each n As String In split
           Log("n: " & n)
       Next
   Next
End Sub

Sub SplitWithAllElements(txt As String) As String()
   txt = txt & ",~"
   Dim s() As String = Regex.Split(",", txt)
   Dim s2(s.Length - 1) As String
   Bit.ArrayCopy(s, 0, s2, 0, s2.Length)
   Return s2
End Sub
 
Upvote 0
Top