Addo Well-Known Member Licensed User Longtime User Oct 3, 2019 #1 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 Click to expand... any idea why ?
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 Click to expand... any idea why ?
klaus Expert Licensed User Longtime User Oct 3, 2019 #2 For i = 0 To paramstr.Length - 1 You must use paramstr.Length - 1 because the index begins with 0. Upvote 0
Addo Well-Known Member Licensed User Longtime User Oct 3, 2019 #3 this should have a Length of 6 if i use paramstr(6) it raises the error Upvote 0
Erel B4X founder Staff member Licensed User Longtime User Oct 3, 2019 #4 klaus said: because the index begins with 0. Click to expand... Upvote 0
DonManfred Expert Licensed User Longtime User Oct 3, 2019 #5 PassionDEV said: this should have a Length of 6 if i use paramstr(6) it raises the error Click to expand... 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 Click to expand... 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
PassionDEV said: this should have a Length of 6 if i use paramstr(6) it raises the error Click to expand... 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 Click to expand... 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
AnandGupta Expert Licensed User Longtime User Oct 3, 2019 #6 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
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