-
Apr 21st, 2020, 07:26 AM
#1
Thread Starter
Addicted Member
[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
-
Apr 21st, 2020, 07:29 AM
#2
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?
-
Apr 21st, 2020, 07:59 AM
#3
Thread Starter
Addicted Member
Re: replace strings in list1
Originally Posted by Arnoutdv
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.
-
Apr 21st, 2020, 08:10 AM
#4
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
-
Apr 21st, 2020, 08:21 AM
#5
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
-
Apr 21st, 2020, 08:48 AM
#6
Thread Starter
Addicted Member
Re: replace strings in list1
Originally Posted by DataMiser
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
-
Apr 21st, 2020, 08:51 AM
#7
Re: replace strings in list1
Add a Trim$() around the Replace$()
-
Apr 21st, 2020, 08:55 AM
#8
Thread Starter
Addicted Member
Re: replace strings in list1
Originally Posted by Arnoutdv
Add a Trim$() around the Replace$()
am new to trimm can you show me
-
Apr 21st, 2020, 09:20 AM
#9
Re: replace strings in list1
Look up the help, come on man!
Just press F1 for the help
-
Apr 21st, 2020, 11:58 AM
#10
Thread Starter
Addicted Member
Re: replace strings in list1
Originally Posted by Arnoutdv
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
-
Apr 21st, 2020, 12:01 PM
#11
Re: replace strings in list1
Originally Posted by doberman2002
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.
-
Apr 21st, 2020, 12:23 PM
#12
Thread Starter
Addicted Member
Re: replace strings in list1
Originally Posted by ChrisE
you didn't bother pressing F1
if that's your answer
f1 brings error
-
Apr 21st, 2020, 12:26 PM
#13
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.
-
Apr 21st, 2020, 04:40 PM
#14
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()
-
Apr 22nd, 2020, 09:12 AM
#15
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
-
Apr 23rd, 2020, 12:21 AM
#16
Thread Starter
Addicted Member
Re: replace strings in list1
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
|