Results 1 to 11 of 11

Thread: multiply char

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Sep 2011
    Posts
    17

    multiply char

    help...
    text1.text = string(5,vbforums)
    result text1.text = vbforumsvbforumsvbforumsvbforumsvbforums

    i use string(5,vbforums) but it don't work
    anyone can help me

  2. #2
    VB-aholic & Lovin' It LaVolpe's Avatar
    Join Date
    Oct 2007
    Location
    Beside Waldo
    Posts
    19,541

    Re: multiply char

    Either concatenate the string "vbforums" 5 times or get fancy using Mid$()
    Code:
    For I = 1 To 5
       theString = theString & "vbforums"
    Next
    
    :: or ::
    
    srcString = "vbforums"
    iLen = Len(srcString)
    theString = Space$(iLen * nrTimesToRepeat)
    For i = 0 to nrTimesToRepeat-1
       Mid$(theString, i*iLen + 1, iLen) = srcString
    Next
    Insomnia is just a byproduct of, "It can't be done"

    Classics Enthusiast? Here's my 1969 Mustang Mach I Fastback. Her sister '67 Coupe has been adopted

    Newbie? Novice? Bored? Spend a few minutes browsing the FAQ section of the forum.
    Read the HitchHiker's Guide to Getting Help on the Forums.
    Here is the list of TAGs you can use to format your posts
    Here are VB6 Help Files online


    {Alpha Image Control} {Memory Leak FAQ} {Unicode Open/Save Dialog} {Resource Image Viewer/Extractor}
    {VB and DPI Tutorial} {Manifest Creator} {UserControl Button Template} {stdPicture Render Usage}

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

    Re: multiply char

    Get even fancier using Mid statement. (Mid$ only applies to the function, though the syntax works with both.)
    Code:
    srcString = "vbforums"
    iLen = Len(srcString)
    theString = Space$(iLen * nrTimesToRepeat)
    Mid(theString, 1) = srcString
    Mid(theString, iLen + 1) = theString ' Believe it! :o)
    I think we had this wrapped up as a function in Code It Better a while back.

    Edit: Not in CIB. This was discussed here in May '08
    The function using Mid as above appeared in a separate thread here in Jan '09
    Quote Originally Posted by Merri
    Here is a function that works just like String$ function, but you can pass strings longer than 1 character:
    Code:
    Public Function Replicate(ByVal Count As Long, ByRef Text As String) As String
        If Len(Text) > 1 Then
            If Count > 1 Then
                Replicate = Space$(Len(Text) * Count)
                Mid$(Replicate, 1, Len(Text)) = Text
                Mid$(Replicate, Len(Text) + 1) = Replicate
            ElseIf Count = 1 Then
                Replicate = Text
            End If
        Else
            Replicate = String$(Count, Text)
        End If
    End Function
    Last edited by Logophobic; Nov 4th, 2011 at 05:53 PM.

  4. #4
    PowerPoster Code Doc's Avatar
    Join Date
    Mar 2007
    Location
    Omaha, Nebraska
    Posts
    2,354

    Re: multiply char

    Consider this possibility:
    Code:
    MyString = "VbForums"
    BigString = Space$(Len(MyString) * 5)
    For I = 1 To 5 * Len(MyString) Step Len(MyString)
        Mid$(BigString, I) = MyString
    Next
    MsgBox BigString
    Doctor Ed

  5. #5
    PowerPoster
    Join Date
    Jul 2006
    Location
    Maldon, Essex. UK
    Posts
    6,334

    Re: multiply char

    I was wondering how to do it without coding a loop and came up with this:
    Code:
    Dim strA As String
    Dim strB As String
    Dim intRepeat As Integer
    strA = "VBForums"
    intRepeat = 5
    strB = String(intRepeat, 0)
    strB = Replace(strB, Chr(0), strA)
    Debug.Print strB
    Last edited by Doogle; Nov 5th, 2011 at 02:58 AM.

  6. #6
    PowerPoster Code Doc's Avatar
    Join Date
    Mar 2007
    Location
    Omaha, Nebraska
    Posts
    2,354

    Re: multiply char

    Good one, Doogle, but loops are rather fast as well.

    It would be interesting to see someone test my code against yours with a billion iterations or so. I'm not sure which one is faster.

    Logophobic, et al. used to do things like that on occasion.
    Doctor Ed

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

    Re: multiply char

    Replace is much slower, though it is fast enough for practical use. The only benefit is that it can be written as one line: strB = Replace(Space$(Number), " ", strA)

    Comparison: "VBForums", repeat 500,000
    Mid, as in Merri's function: 11 ms
    Loop, a la Code Doc: 27 ms
    Replace, one-liner: 3500 ms

  8. #8
    PowerPoster Spoo's Avatar
    Join Date
    Nov 2008
    Location
    Right Coast
    Posts
    2,656

    Re: multiply char

    Logo

    Nice comparison ..

    Spoo

  9. #9

    Thread Starter
    Junior Member
    Join Date
    Sep 2011
    Posts
    17

    Resolved Re: multiply char

    wow...
    thanks for all the reply...


  10. #10
    Junior Member
    Join Date
    Oct 2011
    Location
    Chicago, IL
    Posts
    28

    Re: multiply char

    Damn, that was pretty cool

  11. #11
    PowerPoster Code Doc's Avatar
    Join Date
    Mar 2007
    Location
    Omaha, Nebraska
    Posts
    2,354

    Re: multiply char

    Quote Originally Posted by Logophobic View Post
    Replace is much slower, though it is fast enough for practical use. The only benefit is that it can be written as one line: strB = Replace(Space$(Number), " ", strA)

    Comparison: "VBForums", repeat 500,000
    Mid, as in Merri's function: 11 ms
    Loop, a la Code Doc: 27 ms
    Replace, one-liner: 3500 ms
    Thanks, Logo. Nice to see my code wasn't that slow at that. Simplicity also has some merit.
    Doctor Ed

Tags for this Thread

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