1. *** New version of B4J is available ***
    B4J v7.8
    Dismiss Notice

B4J Question Split

Discussion in 'B4J Questions' started by sdleidel, Aug 18, 2019.

  1. sdleidel

    sdleidel Member Licensed 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
     
  2. ilan

    ilan Expert Licensed User

    yes you are right there is an issue:

    Code:
    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:

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

    Code:
    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
     
  3. Erel

    Erel Administrator Staff Member Licensed User

  4. sdleidel

    sdleidel Member Licensed 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...
     
  5. emexes

    emexes Well-Known Member Licensed User

    This works for your demo data, returns only numbers, no blank entries?
    Code:
    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
    Code:
    Program started.
    [
    12]
    [
    23]
    [
    33]
    [
    1]
    [
    14]
    [
    3]
    [
    18]
     
    Last edited: Aug 18, 2019
  6. Erel

    Erel Administrator Staff Member Licensed User

    He wants the empty elements to be kept.

    Code:
    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 StringAs String()
       txt = txt & 
    ",~"
       
    Dim s() As String = Regex.Split(",", txt)
       
    Dim s2(s.Length - 1As String
       
    Bit.ArrayCopy(s, 0, s2, 0, s2.Length)
       
    Return s2
    End Sub
     
    Johan Hormaza and DonManfred like this.
  7. emexes

    emexes Well-Known Member Licensed User

    Sorry, I got confused by the filtering-out of empty elements in post #2.
     
  8. sdleidel

    sdleidel Member Licensed User

    Perfect (-:
    Thank you.



     
Loading...
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice