Results 1 to 15 of 15

Thread: [HELP] String Manipulation

  1. #1

    Thread Starter
    New Member
    Join Date
    Nov 2012
    Posts
    8

    Question [HELP] String Manipulation

    hi all
    i hve a little problem..
    i wanna make simple function to generate/manipulate a string.

    Example :
    Input string :
    vbforums
    Output string must be :
    v.bforums
    vb.forums
    vbf.orums
    vbfo.rums
    vbfor.ums
    vbforu.ms
    vbforum.s
    v.b.forums
    v.bf.orums
    v.bfor.ums
    ..etc as posible string(no double)


    look simple but a confused with this

    please someone help me

  2. #2
    PowerPoster SamOscarBrown's Avatar
    Join Date
    Aug 2012
    Location
    NC, USA
    Posts
    9,622

    Re: [HELP] String Manipulation

    well, down to the double period, here's the solution...I'll work on the remainder in a moment.

    Private Sub Command1_Click()

    dim myText as string, myNewText as string, x as integer
    myText = Text1.Text
    For X = 1 To Len(Text1.Text)
    myNewText = myNewText + Mid(myText, 1, X) + "." + Mid(myText, X + 1) + vbCr
    Next X
    End Sub

  3. #3

    Thread Starter
    New Member
    Join Date
    Nov 2012
    Posts
    8

    Re: [HELP] String Manipulation

    Quote Originally Posted by SamOscarBrown View Post
    well, down to the double period, here's the solution...I'll work on the remainder in a moment.

    Private Sub Command1_Click()

    dim myText as string, myNewText as string, x as integer
    myText = Text1.Text
    For X = 1 To Len(Text1.Text)
    myNewText = myNewText + Mid(myText, 1, X) + "." + Mid(myText, X + 1) + vbCr
    Next X
    End Sub

    thanks fo ur reply.
    thats work but i need more output
    whit your function output text just :
    v.bforums
    vb.forums
    vbf.orums
    vbfo.rums
    vbfor.ums
    vbforu.ms
    vbforum.s

    i mean all posible .(dot) watever 1 or 2 or many dot whit word : vbforums
    like this:

    v.bforums
    vb.forums
    vbf.orums
    vbfo.rums
    vbfor.ums
    vbforu.ms
    vbforum.s
    v.b.forums
    v.bf.orums
    v.bfor.ums
    v.bforu.ms
    v.bforum.s
    v.b.f.orums
    .
    .
    .
    vbforu.m.s
    .
    .
    .
    until
    v.b.f.o.r.u.m.s

    and i wanna add output text to Listview.
    any idea?
    please someone help me

    Sorry for my grammar look bad..

  4. #4
    Hyperactive Member Lenggries's Avatar
    Join Date
    Sep 2009
    Posts
    353

    Re: [HELP] String Manipulation

    I suggest you think about this problem a little differently. Given any word of n letters, you have (n-1) slots where a period could appear. Think of those slots like binary number with (n-1) digits. So in the case of "vbforums", where n = 8, the binary number with which you would map the periods would consist of seven digits. Thus:
    0000000 = "vbforums"
    1000000 = "v.bforums"
    ...
    0100010 = "vb.foru.ms"
    ...
    1111111 = "v.b.f.o.r.u.m.s"

    Thus, you have 2^(n-1) permutations of letters and periods. To generate your list, simply iterate from 1 to 2^(n-1) and generate the string accordingly. After that, if you want to place the string in some sort of order, sort it according to whatever rule you want (you could also re-code the mapping so that it generates the results in the correct order if you like, but that would probably be more difficult, although it would run faster).

    So that's how to solve the problem. Now it's your job to actually solve it.

  5. #5
    PowerPoster
    Join Date
    Jun 2001
    Location
    Trafalgar, IN
    Posts
    4,141

    Re: [HELP] String Manipulation

    Based on Lenggries' idea, this should work.
    Code:
    Option Explicit
    
    Private Sub Command1_Click()
    Dim i As Integer
    Dim ub As Integer
    Dim intMaskLen As Integer
    
        intMaskLen = Len(Text1.Text) - 1
        ub = 2 ^ intMaskLen
        
        For i = 1 To ub - 1
            Debug.Print FormatValue(Text1.Text, NumberToBinary(i, intMaskLen))
        Next
    End Sub
    
    Private Function NumberToBinary(ByVal sInput As Long, ByVal iMaskLen) As String
    Dim lngTemp As Long
    Dim strBinary As String
    Dim strMask As String
    
        Do
            lngTemp = sInput Mod 2
            strBinary = CStr(lngTemp) & strBinary
            sInput = sInput \ 2
        Loop Until sInput = 0
    
        strBinary = String(iMaskLen, "0") & strBinary
        strBinary = Right(strBinary, iMaskLen)
        
        NumberToBinary = strBinary
    End Function
    
    
    Private Function FormatValue(ByVal strInput As String, ByVal strMask As String) As String
    Dim i As Integer
    Dim strRet As String
        
        For i = 1 To Len(strInput)
            strRet = strRet & Mid(strInput, i, 1)
            If i <= Len(strMask) Then
                If Mid(strMask, i, 1) = 1 Then
                    strRet = strRet & "."
                End If
            End If
        Next i
    
        FormatValue = strRet
        
    End Function

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

    Re: [HELP] String Manipulation

    This works, I used a list box rather than ListView. You should be able to easily modify it for adding to a ListView instead:
    Code:
    Dim ListPoint As Integer, Pointer As Integer, StrLn
    RootStr = "vbforums"
    ' Establish initial list elements with one period first
    For I = 1 To Len(RootStr) - 1
        List1.AddItem Left$(RootStr, I) & "." & Mid$(RootStr, I + 1)
    Next
    ' Now add elements to the list iteratively based on the initial list
    For I = 2 To Len(RootStr) - 1
        For J = I To Len(RootStr) - 1
            If ListPoint Then
                List1.AddItem Left$(List1.List(ListPoint), J + StrLn + 1) & "." & Mid$(List1.List(ListPoint + 1), StrLn + J + 2)
            Else: List1.AddItem Left$(List1.List(ListPoint), J + StrLn + 1) & "." & Mid$(List1.List(ListPoint), StrLn + J + 2)
            End If
            Pointer = Pointer + 1
        Next
        ' Increment List Pointer and String Length
        ListPoint = ListPoint + Pointer + 1
        StrLn = StrLn + 1
        Pointer = 0 ' Reset Pointer to zero 
    Next
    Rather simple, don't you think?
    Doctor Ed

  7. #7
    PowerPoster
    Join Date
    Jun 2001
    Location
    Trafalgar, IN
    Posts
    4,141

    Re: [HELP] String Manipulation

    Hey Doc - Your code misses a few possibilities. If your input string is TEST, the result set misses TE.S.T

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

    Re: [HELP] String Manipulation

    So it does. Generalizing for any string length is a challenge.

    OK. I'll work on it. Seems like a simple fix once the pattern is established.
    Last edited by Code Doc; Dec 14th, 2012 at 10:53 PM.
    Doctor Ed

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

    Re: [HELP] String Manipulation

    Quote Originally Posted by MarkT View Post
    Hey Doc - Your code misses a few possibilities. If your input string is TEST, the result set misses TE.S.T
    Mark, I think I got it right this time, I love a challenge. This was a dandy:
    Code:
    Dim XX As Integer, ListPoint As Integer, OffSet As Integer
    MyStr = "vbforums"
    'Build an Initial List
    For I = 1 To Len(MyStr) - 1
        List1.AddItem Left$(MyStr, I) & "." & Mid$(MyStr, I + 1)
    Next
    ' Add non-adjacent periods to each string in the list iteratively
    Do While ListPoint < List1.ListCount - 1
        J = J + 1
        OffSet = J
        For I = ListPoint To List1.ListCount - 2
            XX = InStr(J + OffSet, List1.List(I), ".")
            If XX Then
                List1.AddItem Left$(List1.List(I), XX + 1) & "." & Mid$(List1.List(I), XX + 2)
                OffSet = OffSet + 1
            End If
        Next
        ListPoint = I + 1
    Loop
    I've tested this with strings of different lengths and it appears to work.
    Doctor Ed

  10. #10
    PowerPoster
    Join Date
    Jun 2001
    Location
    Trafalgar, IN
    Posts
    4,141

    Re: [HELP] String Manipulation

    Keep trying

    Using 12345 as your test string see if you have these

    12.34.5
    1.234.5
    1.23.45
    1.23.4.5
    1.2.34.5

    When the string reaches 8 letters you have 99 misses.

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

    Re: [HELP] String Manipulation

    Quote Originally Posted by MarkT View Post
    Keep trying

    Using 12345 as your test string see if you have these

    12.34.5
    1.234.5
    1.23.45
    1.23.4.5
    1.2.34.5

    When the string reaches 8 letters you have 99 misses.
    Using the code in post #9, I get these using 12345 as a string:
    1.2345
    12.345
    123.45
    1234.5
    1.2.345
    12.3.45
    123.4.5
    1.2.3.45
    12.3.4.5
    1.2.3.4.5

    That's not the same as what you posted, but I guess you were listing only potential exceptions. It that true? I assumed that the periods were supposed to be separated by at most one character and never adjacent to each other. Was that an incorrect assumption?
    Last edited by Code Doc; Dec 15th, 2012 at 07:35 PM. Reason: typo
    Doctor Ed

  12. #12
    PowerPoster
    Join Date
    Jun 2001
    Location
    Trafalgar, IN
    Posts
    4,141

    Re: [HELP] String Manipulation

    My take on it was the periods could be anywhere or everywhere in the string.

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

    Re: [HELP] String Manipulation

    Quote Originally Posted by MarkT View Post
    My take on it was the periods could be anywhere or everywhere in the string.
    ... provided they were separated by a character? No two or more periods could be adjacent--correct?
    Now that I look back, OP indicated in his list that at least these were required: v.bf.orums and v.bfor.ums

    So I guess any two periods could be separated by as many as n - 2 characters, but they must be separated by at least one. My code in post #9 restricts the period separation to no more than 1 character. That could be modified. Regardless, I think we need a clarification from OP before devoting anymore time to this.
    Last edited by Code Doc; Dec 15th, 2012 at 11:06 PM.
    Doctor Ed

  14. #14
    Lively Member
    Join Date
    Jun 2010
    Posts
    68

    Re: [HELP] String Manipulation

    Quote Originally Posted by Code Doc View Post
    Mark, I think I got it right this time, I love a challenge. This was a dandy:
    Code:
    Dim XX As Integer, ListPoint As Integer, OffSet As Integer
    MyStr = "vbforums"
    'Build an Initial List
    For I = 1 To Len(MyStr) - 1
        List1.AddItem Left$(MyStr, I) & "." & Mid$(MyStr, I + 1)
    Next
    ' Add non-adjacent periods to each string in the list iteratively
    Do While ListPoint < List1.ListCount - 1
        J = J + 1
        OffSet = J
        For I = ListPoint To List1.ListCount - 2
            XX = InStr(J + OffSet, List1.List(I), ".")
            If XX Then
                List1.AddItem Left$(List1.List(I), XX + 1) & "." & Mid$(List1.List(I), XX + 2)
                OffSet = OffSet + 1
            End If
        Next
        ListPoint = I + 1
    Loop
    I've tested this with strings of different lengths and it appears to work.
    bro,i do not know u will get my reply or not.
    by this code,"vbforums" get 28 combination,but i get 128 variation of it in a php site
    can you make your function more that gets more result?????
    reply on various posts make you a power poster,may not being u a helpful poster,if u dn't understand how u help then dn't need to reply,it increases ur post count,but decreases ur worthness

  15. #15
    Lively Member
    Join Date
    Jun 2010
    Posts
    68

    Re: [HELP] String Manipulation

    Quote Originally Posted by MarkT View Post
    Based on Lenggries' idea, this should work.
    Code:
    Option Explicit
    
    Private Sub Command1_Click()
    Dim i As Integer
    Dim ub As Integer
    Dim intMaskLen As Integer
    
        intMaskLen = Len(Text1.Text) - 1
        ub = 2 ^ intMaskLen
        
        For i = 1 To ub - 1
            Debug.Print FormatValue(Text1.Text, NumberToBinary(i, intMaskLen))
        Next
    End Sub
    
    Private Function NumberToBinary(ByVal sInput As Long, ByVal iMaskLen) As String
    Dim lngTemp As Long
    Dim strBinary As String
    Dim strMask As String
    
        Do
            lngTemp = sInput Mod 2
            strBinary = CStr(lngTemp) & strBinary
            sInput = sInput \ 2
        Loop Until sInput = 0
    
        strBinary = String(iMaskLen, "0") & strBinary
        strBinary = Right(strBinary, iMaskLen)
        
        NumberToBinary = strBinary
    End Function
    
    
    Private Function FormatValue(ByVal strInput As String, ByVal strMask As String) As String
    Dim i As Integer
    Dim strRet As String
        
        For i = 1 To Len(strInput)
            strRet = strRet & Mid(strInput, i, 1)
            If i <= Len(strMask) Then
                If Mid(strMask, i, 1) = 1 Then
                    strRet = strRet & "."
                End If
            End If
        Next i
    
        FormatValue = strRet
        
    End Function
    ya,this one is best
    reply on various posts make you a power poster,may not being u a helpful poster,if u dn't understand how u help then dn't need to reply,it increases ur post count,but decreases ur worthness

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