Android Question Long strings concatenation speed issue

FMC

Member
Licensed User
Hello,

I have to create a very long string by concatenating many (thousands) smaller ones.

Now I use two methods:

1. (The not recommended one):
B4X:
LongString = LongString & SmallString

2. By converting strings into Byte Arrays and performing an ArrayCopy:

B4X:
Sub StringsConcat(Str1 As String, Str2 As String) As String
    Dim StrLen As Int = Str1.Length + Str2.Length
    Dim StrAsBytesArray(StrLen) As Byte
    ByteConv.ArrayCopy(Str1.GetBytes("UTF8"), 0, StrAsBytesArray, 0, Str1.Length)
    ByteConv.ArrayCopy(Str2.GetBytes("UTF8"), 0, StrAsBytesArray, Str1.Length, Str2.Length)
    Return (ByteConv.StringFromBytes(StrAsBytesArray, "UTF8"))
End Sub

and using it like this:

B4X:
LongString = StringsConcat (LongString, SmallString)

I call the concatenation operation once for each of the thousands small strings.
The performance of method 2 is slightly better than method 1 (in my real case from 1m:30s to 1m:10s).
I have to mention that I have to use a string variable without sending it (by using AStream.Write as it was recommended in the other related questions).

Are there other methods to optimize the speed of this concatenation operation ?

Than you.
 

FMC

Member
Licensed User
Thank you very much, stevel05, your fast recommendation was the solution to my problem.
Best regards.
 
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…