Android Question Split String to array

Lello1964

Well-Known Member
Licensed User
Longtime User
I want split a string to array, with fixed lenght .

example :
string ="1234567890aaaaaaaaaabcedsfd"
a(1)="1234567890"
a(2)="aaaaaaaaaa"
a(3)="bcedsfd"

fixed lenght =10
 

rraswisak

Active Member
Licensed User
Longtime User
please try this code;

B4X:
Dim a As List
Dim text = "1234567890aaaaaaaaaabcedsfdXXX"
   
a.Initialize
Do While text.Length > 10
    a.Add(text.SubString2(0,10))
    text = text.SubString(10)
Loop
If text.Length > 0 Then a.Add(text)
For i = 0 To a.Size-1
    Log(a.Get(i))
Next
 
Upvote 0

mc73

Well-Known Member
Licensed User
Longtime User
B4X:
Private myString As String="1234567890aaaaaaaaaabcedsfd"
Private fixedLength As Int=10
   
   Private stringLength As Int=myString.length
   If stringLength>0 Then
       Private lst As List
       lst.initialize
      Private i As Int
       Do While i<stringLength
           Private tempStringLength As Int=Min(stringLength-i,fixedLength)
           Private arrString As String=myString.SubString2(i,i+tempStringLength)
           lst.Add(arrString)
           i=i+tempStringLength
       Loop
       Private arr(lst.Size) As String
       For j=0 To lst.Size-1
           arr(j)=lst.Get(j)
           Log(arr(j))
       Next
   Else
       Log("empty string")
   End If
 
Upvote 0

stevel05

Expert
Licensed User
Longtime User
My ha'penny worth:

B4X:
Public Sub SplitFLString(Source As String, Length As Int) As String()
    'Disallow div by < 1 Return whatever you like
    If Length < 1 Then Return Array As String()' Or maybe Array As String(Source)
    Dim Arr(Ceil(Source.Length / Length)) As String
    Dim StartSplit As Int = 0
    Dim EndSplit As Int = Min(Length,Source.Length)
 
    For i = 0 To Arr.Length - 1
        Arr(i) = Source.SubString2(StartSplit,EndSplit)
        StartSplit = StartSplit + Length
        EndSplit = Min(StartSplit + Length,Source.Length)
    Next
    Return Arr
End Sub

Edit: changed div by 0 test to disallow -ve numbers
 
Last edited:
Upvote 0

sorex

Expert
Licensed User
Longtime User
no fans of the retro style step loop? :)

B4X:
    Dim t As String="1234567890aaaaaaaaaabcedsfd"  
    Dim strings As List
    strings.Initialize
    For x=0 To t.Length-1 Step 10
        strings.Add(t.SubString2(x,Min(x+10,t.Length)))
    Next
 Log(strings)

(ArrayList) [1234567890, aaaaaaaaaa, bcedsfd]

advanced regex version:

B4X:
    Dim regexStrings As List
    regexStrings.Initialize
    Dim m As Matcher=Regex.Matcher("(.{1,10})",t)
    Do While m.Find
        regexStrings.Add(m.Group(1))
    Loop
    Log(regexStrings)

(ArrayList) [1234567890, aaaaaaaaaa, bcedsfd]
 
Upvote 0

Lello1964

Well-Known Member
Licensed User
Longtime User
no fans of the retro style step loop? :)

B4X:
    Dim t As String="1234567890aaaaaaaaaabcedsfd" 
    Dim strings As List
    strings.Initialize
    For x=0 To t.Length-1 Step 10
        strings.Add(t.SubString2(x,Min(x+10,t.Length)))
    Next
 Log(strings)

(ArrayList) [1234567890, aaaaaaaaaa, bcedsfd]

advanced regex version:

B4X:
    Dim regexStrings As List
    regexStrings.Initialize
    Dim m As Matcher=Regex.Matcher("(.{1,10})",t)
    Do While m.Find
        regexStrings.Add(m.Group(1))
    Loop
    Log(regexStrings)

(ArrayList) [1234567890, aaaaaaaaaa, bcedsfd]

yes, that i'm loonking for
 
Upvote 0

Peter Meares

Member
Licensed User
Longtime User
Just as a learning experience I timed three of the options to see which was fastest.
100000 characters cut into lengths of 10 and put them in a List.

Using a string. SubString2 = 5 ticks
Using a char array. CharstoString = 3 ticks
Using a string. Regex = 19 ticks
 
Upvote 0
Top