Android Question How to remove item at position from array ?

Volga_

Member
Hi all,
I want to remove item at position from array, for example:
B4A:
Dim a() As String = Array As String("abc","de","fgh")
Now item at position 1 will be removed, I want to get:
B4A:
a=("de","fgh")
How to do this? Thanks.
 

William Lancee

Well-Known Member
Licensed User
Longtime User
If you want to keep it as an Array, you could...

B4X:
'    Dim a() As String = Array As String("abc","de","fgh")
'    a = StrArrayRemoveAt(a, 0)
'    Log(a.Length & TAB & a(0) & TAB & a(1))  'Log: 2    de    fgh
Private Sub StrArrayRemoveAt(a() As String, index As Int) As String()
    Dim result(a.Length - 1) As String
    Dim cnt As Int
    For i = 0 To a.Length - 1
        If i = index Then Continue
        result(cnt) = a(i)
        cnt = cnt + 1
    Next
    Return result
End Sub
 
Upvote 0
Top