Results 1 to 35 of 35

Thread: [RESOLVED] String Concatenation Question...

Threaded View

  1. #13
    Frenzied Member
    Join Date
    Jun 2006
    Posts
    1,098

    Re: String Concatenation Question...

    Hey there
    Perhaps a visual aide will help?

    Repeat(16, "abc")

    dilettante's method, using the Mid() statement:
    Code:
    "................................................"
    "abc............................................."
    "abcabc.........................................."
    "abcabcabc......................................."
    "abcabcabcabc...................................."
    "abcabcabcabcabc................................."
    "abcabcabcabcabcabc.............................."
    "abcabcabcabcabcabcabc..........................."
    "abcabcabcabcabcabcabcabc........................"
    "abcabcabcabcabcabcabcabcabc....................."
    "abcabcabcabcabcabcabcabcabcabc.................."
    "abcabcabcabcabcabcabcabcabcabcabc..............."
    "abcabcabcabcabcabcabcabcabcabcabcabc............"
    "abcabcabcabcabcabcabcabcabcabcabcabcabc........."
    "abcabcabcabcabcabcabcabcabcabcabcabcabcabc......"
    "abcabcabcabcabcabcabcabcabcabcabcabcabcabcabc..."
    "abcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabc"
    anhn's method, using concatenation:
    Code:
    "abcabc"
    "abcabcabcabc"
    "abcabcabcabcabcabcabcabc"
    "abcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabc"
    anhn uses Log to determine how many concatenations are needed and a recursive call to get the exact number of reps.

    anhn's method clearly has fewer steps, but the slowness of concatenation makes the end result only marginally faster. (Or so it seems, I didn't compare them significantly)

    A blend of the two should prove to be faster still:
    Code:
    Function Repeat(ByVal Number As Long, ByVal Text As String) As String
      Dim lngLength As Long
      Dim n As Long
      
      If Number < 1 Then Exit Function
      n = Len(Text)
      lngLength = Number * n
      Repeat = Space(lngLength)
      Mid(Repeat, 1, n) = Text
      Do While n < lngLength
        Mid(Repeat, n + 1, n) = Mid$(Repeat, 1, n)
        n = n + n
      Loop
    End Function
    Last edited by Logophobic; May 27th, 2008 at 09:17 PM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width