Android Question Technics to return a variable size array of string

leongcc

Member
Licensed User
Longtime User
What is the best way to code a function (in B4A) that returns a variable size array of string ?
Using Regex.Split as an example :

B4X:
Sub Split(delimit as string, s as string) as string()
...
    return x()
End Sub

A number of strings are created and they are returned as x().
I would think List is used to store the strings and then converted to an array before returning ?
Or Is x() continuously being re-dimensioned during processing ?
Or The strings are stored a file and then read-back into an array ?
 

DonManfred

Expert
Licensed User
Longtime User
B4X:
Sub Split(delimit as string, s as string) as string()
    ' calculate the amount of items you need to return
    dim count as int = 10 ' 10 in this example
    dim x(count) as string
    ' Fill x(0) to x(9) .....

    return x
End Sub
 
Upvote 0

leongcc

Member
Licensed User
Longtime User
B4X:
Sub Split(delimit as string, s as string) as string()
   '***
 ' calculate the amount of items you need to return
    dim count as int = 10 ' 10 in this example
    dim x(count) as string
    ' Fill x(0) to x(9) .....

    return x
End Sub

Thanks for responding.
Actually my query is more about the portion of codes marked with '***'.
While calculating the amount of items I need to return, I will at the same time capturing the item 1 by 1.
This is happening before I declare the array 'Dim x(count) as string'.
From your expert opinion, where should the captured items to be stored before they are loaded to the array for returning.

Is it better to do this:
' create a large array
Dim x(99999) as string
'... capture items to x and count it, let say count=10
'redim array
dim x(count) as string
return x​

Or do this:
' create a list
Dim y as list
'... capture items to y, let say count = list size = 10
'create array with list size
dim x(count) as string
' Fill x(0) to x(count-1) .....
return x​
 
Upvote 0

leongcc

Member
Licensed User
Longtime User
I have been using ready made functions that returns array of strings, such as Regex.Split().

I know how they can be coded in C, but I am curious how they can be properly coded in B4A.
 
Upvote 0

klaus

Expert
Licensed User
Longtime User
Is it better to do this:
' create a large array
Dim x(99999) as string
'... capture items to x and count it, let say count=10
'redim array
dim x(count) as string
return x
This will not work because when you Dim the array again it is empty!

You will need to either:
- go two times through the capturing, first for the count second for the filling.
- use a List for intermediate storing.
 
Upvote 0
Top