Android Question Initialize a string

Discussion in 'Android Questions' started by realblue, Sep 7, 2015.

  1. realblue

    realblue Member Licensed 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," ")
     
  2. eurojam

    eurojam Well-Known Member Licensed User

    not so short, not very elegant, but should work;)
    Code:
    Dim s As String = ""
      
    For i = 1 To 81920
        s = s & 
    " "
      
    Next
     
  3. realblue

    realblue Member Licensed User

    :) Yes of course but may be there is an efficient way
     
  4. eurojam

    eurojam Well-Known Member Licensed User

    use stringfunctions lib: AddSpaces (HowMany As Int) As String
     
  5. eurojam

    eurojam Well-Known Member Licensed User

  6. realblue

    realblue Member Licensed User

    Thank you eurojam but I rather use for next since the library will load a lot of code that I don't use.
     
  7. Erel

    Erel Administrator Staff Member Licensed 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:
    Code:
    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
     
    DonManfred and eurojam like this.
Loading...
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice