|
-
Nov 4th, 2011, 04:59 PM
#1
Thread Starter
Junior Member
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
-
Nov 4th, 2011, 05:25 PM
#2
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
-
Nov 4th, 2011, 05:35 PM
#3
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
 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.
-
Nov 4th, 2011, 07:26 PM
#4
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
-
Nov 5th, 2011, 02:35 AM
#5
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.
-
Nov 7th, 2011, 09:11 PM
#6
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.
-
Nov 7th, 2011, 11:25 PM
#7
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
-
Nov 8th, 2011, 09:43 AM
#8
Re: multiply char
Logo
Nice comparison .. 
Spoo
-
Nov 8th, 2011, 12:52 PM
#9
Thread Starter
Junior Member
-
Nov 8th, 2011, 12:54 PM
#10
Junior Member
Re: multiply char
Damn, that was pretty cool
-
Nov 8th, 2011, 06:00 PM
#11
Re: multiply char
 Originally Posted by Logophobic
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.
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|