Looking For Letters In Strings
How do i check if a word contains two letters.
VB Code:
Public Sub DoItX(OldLetter As String, OldLetter1 As String, NewLetter As String, NewLetter1 As String)
Dim I As Integer
Dim NewWord As String
For I = 0 To List1.ListCount - 1
[COLOR=Red] If InStr(LCase(List1.List(I)), OldLetter, OldLetter1) Then[/COLOR]
NewWord = List1.List(I)
NewWord = Replace(LCase(NewWord), OldLetter, NewLetter)
NewWord = Replace(LCase(NewWord), OldLetter1, NewLetter1)
List2.AddItem NewWord
End If
Next
End Sub
i tried that but it doesn't like the code in red.
Anyone help ?
Re: Looking For Letters In Strings
InStr has the following signature:
InStr([start As Long], SearchIn As String, SearchFor As String, [compareMethod])
As you can see you can only pass two strings to it. The string you want to search inside (a list item in your case) and the string you search for. What are you trying to do.
Re: Looking For Letters In Strings
If they're next to each other, you can concatenate the string, or just include two string in the first parameter. You can't OR, or AND the results
VB Code:
Public Sub DoItX(OldLetter As String, OldLetter1 As String, NewLetter As String, NewLetter1 As String)
Dim I As Integer
Dim NewWord As String
For I = 0 To List1.ListCount - 1
If InStr(LCase(List1.List(I)), (OldLetter & OldLetter1)) Then
NewWord = List1.List(I)
NewWord = Replace(LCase(NewWord), OldLetter, NewLetter)
NewWord = Replace(LCase(NewWord), OldLetter1, NewLetter1)
List2.AddItem NewWord
End If
Next
End Sub
Re: Looking For Letters In Strings
Im triyng to c if a string contains 2 strings. So how do i do that ?
If i have the word Ricky.
I wana c if the string contains the letters R and I.
Re: Looking For Letters In Strings
the case my b that there not always next to each other.
So is this possible ?
Re: Looking For Letters In Strings
Actually you can run the Replace function without first checking if the string exists. The replace will just return the origional string if it doesn't find a match. Or must both strings exist for you to replace anything?
Re: Looking For Letters In Strings
yeah i know that but i don't want to add words that havn't been changed into list2.
Re: Looking For Letters In Strings
VB Code:
If InStr(LCase(List1.List(I)), OldLetter) and InStr(LCase(List1.list(i)), OldLetter1 Then
Would This Work ?
Re: Looking For Letters In Strings
Again I ask, must both strings exist to replace anything? Or can just one of them be replaced? Must the first be replaced for the second to be replaced?
Re: Looking For Letters In Strings
Yeah for anything to get replaced the string must contain both the letters its looking for.
Re: Looking For Letters In Strings
VB Code:
Public Sub DoItX(OldLetter As String, OldLetter1 As String, NewLetter As String, NewLetter1 As String)
Dim I As Integer
Dim NewWord As String
For I = 0 To List1.ListCount - 1
If InStr(LCase(List1.List(I)), OldLetter) & InStr(LCase(List1.List(I)), OldLetter1) Then
NewWord = List1.List(I)
NewWord = Replace(LCase(NewWord), OldLetter, NewLetter)
NewWord = Replace(LCase(NewWord), OldLetter1, NewLetter1)
List2.AddItem NewWord
End If
Next
End Sub
this works, a bit long winded but it works.
Re: Looking For Letters In Strings
Ricky1 is almost there but you should not use the & operator between the InStr calls since that will do a string concatination of the resulting integers. It should look:
VB Code:
If InStr(1, List1.List(I), OldLetter, vbTextCompare) > 0 And InStr(1, List1.List(I), OldLetter1) > 0 Then
NewWord = Replace(List1.List(I), OldLetter, NewLetter, , , vbTextCompare)
NewWord = Replace(NewWord, OldLetter1, NewLetter1, , , vbTextCompare)
List2.AddItem NewWord
End If
Re: Looking For Letters In Strings
sweet works great. Thanks alot.