Android Question Initialize a string

realblue

Member
Licensed User
Longtime User
Hi everybody,

I like to initialize a string with 10 * 8192 spaces. Is there a simple way like in Visual Basic : StrDup(10*8192," ")
 

eurojam

Well-Known Member
Licensed User
Longtime User
not so short, not very elegant, but should work;)
B4X:
  Dim s As String = ""
  For i = 1 To 81920
    s = s & " "
  Next
 
Upvote 0

realblue

Member
Licensed User
Longtime User
Thank you eurojam but I rather use for next since the library will load a lot of code that I don't use.
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
Strings are immutable objects. You shouldn't concatenate 81920 characters one by one. It will be slow.

You should use StringBuilder instead.

Or another option:
B4X:
Sub CreateSpace(Length As Int) As String
   Dim b(Length) As Byte
   Dim s As Int = Asc(" ")
   For i = 0 To b.Length - 1
     b(i) = s
   Next
   Return BytesToString(b, 0, b.Length, "ASCII")
End Sub
 
Upvote 0
Top