Results 1 to 16 of 16

Thread: [RESOLVED] replace strings in list1

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Aug 2019
    Posts
    194

    Resolved [RESOLVED] replace strings in list1

    replace all strings in list1 matching a string.

    text1.text = love you

    list1 =
    Code:
    love you all now
    genreal love you
    love you tonight 
    i will love you again darling
    love you now forever
    replace all love you

  2. #2
    PowerPoster Arnoutdv's Avatar
    Join Date
    Oct 2013
    Posts
    5,906

    Re: replace strings in list1

    Look up the help for the Replace() function
    I assume you know how to iterate through all items of a Listbox?

  3. #3

    Thread Starter
    Addicted Member
    Join Date
    Aug 2019
    Posts
    194

    Re: replace strings in list1

    Quote Originally Posted by Arnoutdv View Post
    Look up the help for the Replace() function
    I assume you know how to iterate through all items of a Listbox?
    Code:
    Private Sub Command2_Click()
    Dim i As Long
    On Error Resume Next
    For i = 0 To List1.ListCount - 1
        If InStr(List1.List(i), Text1.Text) Then
    List1.List(i) = Replace(List1.List(i), Text1, "")
    
        End If
    Next
    End Sub
    Text1.text = love you
    it removes the first item., it should have removed all of it

    What i think its wrong is the find string cannot detect the main ( string ) in a sentence mixed with other string. Target love you but mixed with love you xxx xxx string

    even tried this says only found in index = 0
    Code:
    Private Const LB_ERR = -1
    Private Const LB_FINDSTRINGEXACT = &H1A2
    Private Const LB_FINDSTRING = &H18F
    
    Private Declare Function SendMessage Lib "USER32" _
        Alias "SendMessageA" (ByVal hWnd As Long _
        , ByVal wMsg As Long _
        , ByVal wParam As Integer _
        , ByVal lParam As Any) As Long
    
    Public Function GetListBoxIndex(hWnd As Long _
        , SearchKey As String _
        , StartIndex As Long _
        , Optional FindExactMatch As Boolean = True) As Long
        If FindExactMatch Then
            GetListBoxIndex = SendMessage(hWnd, LB_FINDSTRINGEXACT, StartIndex, SearchKey)
        Else
            GetListBoxIndex = SendMessage(hWnd, LB_FINDSTRING, StartIndex, SearchKey)
             Debug.Print GetListBoxIndex
        End If
        
       
    End Function
    
    Private Sub PrintAllMatches(hWnd As Long, SearchKey As String)
        Dim firstMatch As Long, nextMatch As Long
        nextMatch = GetListBoxIndex(hWnd, SearchKey, -1, False)
        If nextMatch = LB_ERR Then
            Debug.Print "Not found"
            Exit Sub
        End If
    
        firstMatch = nextMatch
        Do
            Debug.Print "Match is at index " & nextMatch
            nextMatch = GetListBoxIndex(hWnd, SearchKey, nextMatch, False)
        Loop While nextMatch <> firstMatch
    End Sub
    Private Sub Command4_Click()
    GetListBoxIndex List1.hWnd, Text1, 0, False
    End Sub
    Last edited by doberman2002; Apr 21st, 2020 at 08:09 AM.

  4. #4
    PowerPoster
    Join Date
    Feb 2012
    Location
    West Virginia
    Posts
    14,205

    Re: replace strings in list1

    Instr as used will be case sensitive but yes it will find love you if it exists anywhere in the string no matter where it is as long as all those characters appear together as they are in your search string.

    That said you do not really even need the If test. Replace will replace only that phrase and only if it exists.

    Get rid of the On Error Resume Next

  5. #5
    PowerPoster
    Join Date
    Feb 2012
    Location
    West Virginia
    Posts
    14,205

    Re: replace strings in list1

    I tried your code and it works as expected. I removed the On Error Resume next as it should not have been there not that it would have caused a problem just that it should not be used in any but limited cases.

    Code:
    Option Explicit
    Private Sub Form_Load()
    List1.AddItem "love you all now"
    List1.AddItem "genreal love you"
    List1.AddItem "love you tonight"
    List1.AddItem "i will love you again darling"
    List1.AddItem "love you now forever"
    
    Text1.Text = "love you"
    End Sub
    Private Sub Command1_Click()
    Dim i As Long
    
    For i = 0 To List1.ListCount - 1
        If InStr(List1.List(i), Text1.Text) Then
            List1.List(i) = Replace(List1.List(i), Text1.Text, "")
        End If
    Next
    It also works without the If test

  6. #6

    Thread Starter
    Addicted Member
    Join Date
    Aug 2019
    Posts
    194

    Re: replace strings in list1

    Quote Originally Posted by DataMiser View Post
    I tried your code and it works as expected. I removed the On Error Resume next as it should not have been there not that it would have caused a problem just that it should not be used in any but limited cases.

    Code:
    Option Explicit
    Private Sub Form_Load()
    List1.AddItem "love you all now"
    List1.AddItem "genreal love you"
    List1.AddItem "love you tonight"
    List1.AddItem "i will love you again darling"
    List1.AddItem "love you now forever"
    
    Text1.Text = "love you"
    End Sub
    Private Sub Command1_Click()
    Dim i As Long
    
    For i = 0 To List1.ListCount - 1
        If InStr(List1.List(i), Text1.Text) Then
            List1.List(i) = Replace(List1.List(i), Text1.Text, "")
        End If
    Next
    It also works without the If test

  7. #7
    PowerPoster Arnoutdv's Avatar
    Join Date
    Oct 2013
    Posts
    5,906

    Re: replace strings in list1

    Add a Trim$() around the Replace$()

  8. #8

    Thread Starter
    Addicted Member
    Join Date
    Aug 2019
    Posts
    194

    Re: replace strings in list1

    Quote Originally Posted by Arnoutdv View Post
    Add a Trim$() around the Replace$()
    am new to trimm can you show me

  9. #9
    PowerPoster Arnoutdv's Avatar
    Join Date
    Oct 2013
    Posts
    5,906

    Re: replace strings in list1

    Look up the help, come on man!
    Just press F1 for the help

  10. #10

    Thread Starter
    Addicted Member
    Join Date
    Aug 2019
    Posts
    194

    Re: replace strings in list1

    Quote Originally Posted by Arnoutdv View Post
    Look up the help, come on man!
    Just press F1 for the help
    Code:
    List1.List(i) = Replace(List1.List(i), Text1.Text, "")
    is that the right way

  11. #11
    PowerPoster ChrisE's Avatar
    Join Date
    Jun 2017
    Location
    Frankfurt
    Posts
    3,048

    Re: replace strings in list1

    Quote Originally Posted by doberman2002 View Post
    Code:
    List1.List(i) = Replace(List1.List(i), Text1.Text, "")
    is that the right way
    you didn't bother pressing F1
    if that's your answer
    to hunt a species to extinction is not logical !
    since 2010 the number of Tigers are rising again in 2016 - 3900 were counted. with Baby Callas it's 3901, my wife and I had 2-3 months the privilege of raising a Baby Tiger.

  12. #12

    Thread Starter
    Addicted Member
    Join Date
    Aug 2019
    Posts
    194

    Re: replace strings in list1

    Quote Originally Posted by ChrisE View Post
    you didn't bother pressing F1
    if that's your answer
    f1 brings error

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

    Re: replace strings in list1

    Yeah, F1 may not work if the msdn library wasn't installed.

    In my signature below is a link to msdn 'on-line' help files for VB6. You may want to bookmark it. Even to this day, I hit that site a couple times a month to refresh my memory on this or 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}

  14. #14
    PowerPoster
    Join Date
    Feb 2012
    Location
    West Virginia
    Posts
    14,205

    Re: replace strings in list1

    It is a simple method just typing in the word trim in the ide should give you enough of a hint to figure it out but here is a simple example.

    Code:
    MyString=Trim(MyString)
    The reason you were seeing those spaces is because replace replaces exactly what you tell it to replace and since you have a space in the original string that is not part of the replacement criteria it is left untouched which is exactly how it should happen.

    For trimming you have 3 methods LTrim() RTrim() and Trim()

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

    Re: replace strings in list1

    dubiousman....If you want to get rid of the spaces left after removing text1.text (trimmed of spaces), simply try this...

    Code:
    Private Sub Command1_Click()
        Dim i As Long
        For i = 0 To List1.ListCount - 1
            If InStr(List1.List(i), Trim(Text1.Text)) Then
                List1.List(i) = Replace(Trim(Replace(List1.List(i), Trim(Text1.Text), "")), "  ", " ")
            End If
        Next
    End Sub
    EDIT: Notice that one of the Replace functions will replace any double-space left in a line with a single space

  16. #16

    Thread Starter
    Addicted Member
    Join Date
    Aug 2019
    Posts
    194

    Re: replace strings in list1

    thank you sam. and all

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