Android Question why this string raise an error on split

Addo

Well-Known Member
Licensed User
i am trying to split this string

B4X:
14~106~GC2~~700~0~~

using the following code

B4X:
Dim paramstr() As String = Regex.Split("\~", "14~106~GC2~~700~0~~")

For i = 0 To paramstr.Length
Log(paramstr(i))
Next


i got this arrayindexoutofbound error

java.lang.ArrayIndexOutOfBoundsException: length=6; index=6

any idea why ?
 

DonManfred

Expert
Licensed User
Longtime User
this should have a Length of 6 if i use paramstr(6) it raises the error
B4X:
    Dim paramstr() As String = Regex.Split("\~", "14~106~GC2~~700~0~~")
    For i = 0 To paramstr.Length-1
        Log($"Par #${i}:${paramstr(i)}"$)
    Next

results in

Par #0:14
Par #1:106
Par #2:GC2
Par #3:
Par #4:700
Par #5:0

It HAS 6 entries. You need to remember that the index starts with 0 as mentioned two times now.

The 6th Item has Index 5
 
Upvote 0

AnandGupta

Expert
Licensed User
Longtime User
Hi PassionDEV,

It happens if you are coming from another language.
Though the .length gives actual length which matches with other languages, but the index starts from 0 so you will have to check up to .length-1.

Regards,

Anand
 
Upvote 0

Similar Threads

Top