Results 1 to 21 of 21

Thread: [RESOLVED] Repeating first 2 characters?

  1. #1

    Thread Starter
    Frenzied Member
    Join Date
    Dec 2007
    Posts
    1,072

    Resolved [RESOLVED] Repeating first 2 characters?

    I'm trying to find the fastest way to do this. I've written a function where you input a string of numbers and it takes the first 2 characters and repeats them until the new string has the same length of the input string.

    So input = 56378, output = 56565. Input=89432, output=89898

    This is what I have now:
    Code:
    Private Function Alternate(Num As String) As String
    Dim first(0 To 1) As String, x As Long
        first(1) = Mid$(Num, 1, 1)
        first(0) = Mid$(Num, 2, 1)
        For x = 1 To Len(Num)
            Alternate = Alternate & first(x Mod 2)
        Next x
    End Function
    Can you guys think of anything faster?

  2. #2
    Addicted Member
    Join Date
    Oct 2009
    Posts
    164

    Re: Repeating first 2 characters?

    very small point,
    do not use len(num) as the loop condition. put it into a var, and use the var
    also, outside the loop, set the first two chars, then your loop only needs to be for 3 to iLength..

    and I suppose, if you want to nitpick speed and the length will be constant, then you can ditch the loop and code the entire new string in one line. But that wouldn't be very flexible.

    And maybe some others know, would Left() be faster than a Mid()?

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

    Re: Repeating first 2 characters?

    Maybe this?
    Code:
    Private Function Alternate(Num As String) As String
        Dim sPrefix As String, lLen As Long, X As Long
        sPrefix = Left$(Num, 2)
        lLen = Len(Num)
        Alternate = Space$(lLen)
        For X = 1 To lLen - 1 Step 2
            Mid$(Alternate, X, 2) = sPrefix
        Next X
        If (lLen And 1) Then Mid$(Alternate, lLen, 1) = Left$(sPrefix, 1)
    End Function
    This method does not resize the string during/after the loop
    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}

  4. #4
    VB6, XHTML & CSS hobbyist Merri's Avatar
    Join Date
    Oct 2002
    Location
    Finland
    Posts
    6,654

    Re: Repeating first 2 characters?

    Replicate:
    Code:
    Dim Test As String
    
    Test = "AB" & Space$(8)
    Mid$(Test, 3) = Test
    
    MsgBox Test
    I think this works the fastest when the start length has 8 characters or so, I haven't fully benchmarked it. But longer than that and it'll become slower.

  5. #5

    Thread Starter
    Frenzied Member
    Join Date
    Dec 2007
    Posts
    1,072

    Re: Repeating first 2 characters?

    Quote Originally Posted by Golgo1 View Post
    very small point,
    do not use len(num) as the loop condition. put it into a var, and use the var
    also, outside the loop, set the first two chars, then your loop only needs to be for 3 to iLength..

    and I suppose, if you want to nitpick speed and the length will be constant, then you can ditch the loop and code the entire new string in one line. But that wouldn't be very flexible.

    And maybe some others know, would Left() be faster than a Mid()?
    The len as a loop condition is only executed once, as seen in this snippet:
    Code:
    Private Sub TestLoop()
    Dim x As Long
        For x = 1 To GetLoop
            DoEvents
        Next x
    End Sub
    
    Private Function GetLoop() As Long
        Debug.Print "GetLoop called!"
        GetLoop = 5
    End Function
    Call TestLoop and you'll see that GetLoop (the loop condition) is called once.
    Quote Originally Posted by LaVolpe View Post
    Maybe this?
    Code:
    Private Function Alternate(Num As String) As String
        Dim sPrefix As String, lLen As Long, X As Long
        sPrefix = Left$(Num, 2)
        lLen = Len(Num)
        Alternate = Space$(lLen)
        For X = 1 To lLen - 1 Step 2
            Mid$(Alternate, X, 2) = sPrefix
        Next X
        If (lLen And 1) Then Mid$(Alternate, lLen, 1) = Left$(sPrefix, 1)
    End Function
    This method does not resize the string during/after the loop
    LaVolpe you are a genius in every way. I'm gonna see if others have different responses just for kicks

  6. #6
    Addicted Member
    Join Date
    Oct 2009
    Posts
    164

    Re: Repeating first 2 characters?

    actually... thinking about it more, you can take the Mod out entirely

    Code:
    Private Function Alternate(Num As String) As String
    dim strPair as string,  x As Long, L as long
    
    strPair = Mid$(Num, 1, 1) & Mid$(Num, 2, 1)  'combine chars
    L = Len(Num) /2
    
    For x = 1 To L
            Alternate = Alternate & strPair
    Next x
    
    'if orginal Num was an ODD numbered length, trim last digit
    if len(Alternate) > len(Num) then  Alternate = left(Alternate,len(Alternate)-1)
    
    End Function
    Of course, this doest take into account spaces or empty strings (use Trim() )

  7. #7

    Thread Starter
    Frenzied Member
    Join Date
    Dec 2007
    Posts
    1,072

    Re: Repeating first 2 characters?

    And yes, Left is a little faster than Mid:

  8. #8

    Thread Starter
    Frenzied Member
    Join Date
    Dec 2007
    Posts
    1,072

    Re: Repeating first 2 characters?

    Quote Originally Posted by Golgo1 View Post
    actually... thinking about it more, you can take the Mod out entirely

    ...

    Of course, this doest take into account spaces or empty strings (use Trim() )
    I rewrote it for ya
    Code:
    Private Function Alternate(Num As String) As String
    dim strPair as string,  x As Long
    
    strPair = Left$(Num,2)  'combine chars
    
    For x = 1 To LenB(Num) \ 4
            Alternate = Alternate & strPair
    Next x
    
    'if orginal Num was an ODD numbered length, trim last digit
    if lenb(Alternate) > lenb(Num) then  Alternate = left$(Alternate,len(Alternate)-1)
    
    End Function

  9. #9
    VB6, XHTML & CSS hobbyist Merri's Avatar
    Join Date
    Oct 2002
    Location
    Finland
    Posts
    6,654

    Re: Repeating first 2 characters?

    My earlier suggestion as a function:
    Code:
    Option Explicit
    
    Public Function Alternate(Expression As String, Optional ByVal Length As Long = 2) As String
        Select Case Length
        Case Is < 1
        Case 1
            If Len(Expression) Then Alternate = String$(Len(Expression), Left$(Expression, 1))
        Case Else
            Alternate = Expression
            If Length < Len(Expression) Then Mid$(Alternate, 1 + Length) = Alternate
        End Select
    End Function
    
    Private Sub Form_Load()
        Debug.Print Alternate("1234567890", 3)
    End Sub
    Last edited by Merri; Jul 21st, 2010 at 10:56 PM.

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

    Re: Repeating first 2 characters?

    Very nice Merri. I didn't even know Mid$ did an "auto-fill" like that!
    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}

  11. #11
    Addicted Member
    Join Date
    Oct 2009
    Posts
    164

    Re: Repeating first 2 characters?

    ah I see LV posted while I was typing

    pretty much the same idea though, make a prefix and do half the loops

    And yes, you are right, with a FOR loop, the condition values are set once. My bad, I got it mixed up with a WHILE loop.

    That is a pretty handy little graph, I had no idea Str$ was THAT much slower. (slower yes, but that's alot)

  12. #12
    Addicted Member
    Join Date
    Oct 2009
    Posts
    164

    Re: Repeating first 2 characters?

    holy hell, I agree! using Mid like that is sweet. I never knew it did that either!
    And the Select Case takes care of most of the validation. Slightly longer code, but gauranteed to be faster.
    Nice

  13. #13
    VB6, XHTML & CSS hobbyist Merri's Avatar
    Join Date
    Oct 2002
    Location
    Finland
    Posts
    6,654

    Re: Repeating first 2 characters?

    A slight update to account for errors. Should be bullet proof, no error with any possible input parameters.
    Code:
    Public Function Alternate(Expression As String, Optional ByVal Length As Byte = 2) As String
        If Len(Expression) = 0 Then Exit Function
        Select Case Length
        Case 1
            Alternate = String$(Len(Expression), Left$(Expression, 1))
        Case Else
            Alternate = Expression
            If Length < Len(Expression) Then Mid$(Alternate, 1 + Length) = Alternate
        End Select
    End Function
    Len(Expression) check makes sure we do not get an error with Length = 1 situation when Left$ is called.

    Length is now a Byte instead of Long, thus throwing negative value out of the picture -> no need to check for it!

    Length must be shorter than Len(Expression) for the Mid$ to run. This avoids an error.

    Behavior change: if Length = 0 the code returns the input string instead of a null string.


    But notice that in many cases it would be better to use Mid$ inline instead of calling this function...

  14. #14

    Thread Starter
    Frenzied Member
    Join Date
    Dec 2007
    Posts
    1,072

    Re: Repeating first 2 characters?

    One speed change:
    Code:
        If LenB(Expression) = 0 Then Exit Function
    That saves me 8 nanoseconds

  15. #15
    VB6, XHTML & CSS hobbyist Merri's Avatar
    Join Date
    Oct 2002
    Location
    Finland
    Posts
    6,654

    Re: Repeating first 2 characters?

    Can't use LenB, because afaik it would give an error on Left$ if Expression's byte length is 1... so shorter to use Len instead & having better error validation at the same time.

    A better speed optimization would be to use an extra variable to hold the result of Len.

  16. #16
    Head Hunted anhn's Avatar
    Join Date
    Aug 2007
    Location
    Australia
    Posts
    3,669

    Re: Repeating first 2 characters?

    Shorter:
    Code:
    Function Alternate(ByVal sNum As String, ByVal iLen As Byte) As String
        If Len(sNum) > iLen Then Mid$(sNum, iLen + 1) = sNum
        Alternate = sNum
    End Function
    • Don't forget to use [CODE]your code here[/CODE] when posting code
    • If your question was answered please use Thread Tools to mark your thread [RESOLVED]
    • Don't forget to RATE helpful posts

    • Baby Steps a guided tour
    • IsDigits() and IsNumber() functions • Wichmann-Hill Random() function • >> and << functions for VB • CopyFileByChunk

  17. #17

    Thread Starter
    Frenzied Member
    Join Date
    Dec 2007
    Posts
    1,072

    Re: [RESOLVED] Repeating first 2 characters?

    Merri & anhn, you know never fail to amaze.

  18. #18
    Head Hunted anhn's Avatar
    Join Date
    Aug 2007
    Location
    Australia
    Posts
    3,669

    Re: [RESOLVED] Repeating first 2 characters?

    In case you want to repeat more than 255 characters:
    Code:
    Function RepeatLeft(sText As String, ByVal iLen As Long) As String
        RepeatLeft = sText
        If (iLen > 0) And (Len(sText) > iLen) Then Mid$(RepeatLeft, iLen + 1) = RepeatLeft
    End Function
    • Don't forget to use [CODE]your code here[/CODE] when posting code
    • If your question was answered please use Thread Tools to mark your thread [RESOLVED]
    • Don't forget to RATE helpful posts

    • Baby Steps a guided tour
    • IsDigits() and IsNumber() functions • Wichmann-Hill Random() function • >> and << functions for VB • CopyFileByChunk

  19. #19
    VB6, XHTML & CSS hobbyist Merri's Avatar
    Join Date
    Oct 2002
    Location
    Finland
    Posts
    6,654

    Re: [RESOLVED] Repeating first 2 characters?

    anhn: Mid$ is unable to repeat one character. You need two characters minimum. So iLen > 1 to avoid an extra call to Mid$ or you need to go back to Select Case&#160;or If Then structure, which is already used in post #9.

  20. #20
    Head Hunted anhn's Avatar
    Join Date
    Aug 2007
    Location
    Australia
    Posts
    3,669

    Re: [RESOLVED] Repeating first 2 characters?

    I have this:
    Code:
    ?RepeatLeft("abcd",1)
    aaaa
    • Don't forget to use [CODE]your code here[/CODE] when posting code
    • If your question was answered please use Thread Tools to mark your thread [RESOLVED]
    • Don't forget to RATE helpful posts

    • Baby Steps a guided tour
    • IsDigits() and IsNumber() functions • Wichmann-Hill Random() function • >> and << functions for VB • CopyFileByChunk

  21. #21
    VB6, XHTML & CSS hobbyist Merri's Avatar
    Join Date
    Oct 2002
    Location
    Finland
    Posts
    6,654

    Re: [RESOLVED] Repeating first 2 characters?

    Does not work for me:
    Code:
    ? RepeatLeft("abcdefghijkl", 1)
    aabbddffhhjj
    I have Visual Basic 6.0 (SP6) version 6.0.9782


    This works:
    Code:
    Function RepeatLeft(sText As String, ByVal iLen As Long) As String
        RepeatLeft = sText
        If Len(sText) > iLen Then
            If iLen = 1 Then Mid$(RepeatLeft, 2, 1) = RepeatLeft: iLen = 2
            If iLen > 1 Then Mid$(RepeatLeft, iLen + 1) = RepeatLeft
        End If
    End Function
    Last edited by Merri; Jul 22nd, 2010 at 08:40 AM.

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