Results 1 to 13 of 13

Thread: Looking For Letters In Strings

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Feb 2005
    Posts
    300

    Resolved Looking For Letters In Strings

    How do i check if a word contains two letters.
    VB Code:
    1. Public Sub DoItX(OldLetter As String, OldLetter1 As String, NewLetter As String, NewLetter1 As String)
    2.  
    3. Dim I As Integer
    4. Dim NewWord As String
    5.  
    6. For I = 0 To List1.ListCount - 1
    7. [COLOR=Red]    If InStr(LCase(List1.List(I)), OldLetter, OldLetter1) Then[/COLOR]
    8.         NewWord = List1.List(I)
    9.         NewWord = Replace(LCase(NewWord), OldLetter, NewLetter)
    10.         NewWord = Replace(LCase(NewWord), OldLetter1, NewLetter1)
    11.         List2.AddItem NewWord
    12.     End If
    13. Next
    14.  
    15. End Sub

    i tried that but it doesn't like the code in red.

    Anyone help ?
    Last edited by Ricky1; Sep 24th, 2005 at 07:45 AM.
    Im Learning !!!!

  2. #2
    I'm about to be a PowerPoster! Joacim Andersson's Avatar
    Join Date
    Jan 1999
    Location
    Sweden
    Posts
    14,649

    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.

  3. #3
    Banned dglienna's Avatar
    Join Date
    Jun 2004
    Location
    Center of it all
    Posts
    17,901

    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:
    1. Public Sub DoItX(OldLetter As String, OldLetter1 As String, NewLetter As String, NewLetter1 As String)
    2.  
    3. Dim I As Integer
    4. Dim NewWord As String
    5.  
    6. For I = 0 To List1.ListCount - 1
    7.     If InStr(LCase(List1.List(I)), (OldLetter & OldLetter1)) Then
    8.         NewWord = List1.List(I)
    9.         NewWord = Replace(LCase(NewWord), OldLetter, NewLetter)
    10.         NewWord = Replace(LCase(NewWord), OldLetter1, NewLetter1)
    11.         List2.AddItem NewWord
    12.     End If
    13. Next
    14.  
    15. End Sub

  4. #4

    Thread Starter
    Hyperactive Member
    Join Date
    Feb 2005
    Posts
    300

    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.
    Im Learning !!!!

  5. #5

    Thread Starter
    Hyperactive Member
    Join Date
    Feb 2005
    Posts
    300

    Re: Looking For Letters In Strings

    the case my b that there not always next to each other.

    So is this possible ?
    Im Learning !!!!

  6. #6
    I'm about to be a PowerPoster! Joacim Andersson's Avatar
    Join Date
    Jan 1999
    Location
    Sweden
    Posts
    14,649

    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?

  7. #7

    Thread Starter
    Hyperactive Member
    Join Date
    Feb 2005
    Posts
    300

    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.
    Im Learning !!!!

  8. #8

    Thread Starter
    Hyperactive Member
    Join Date
    Feb 2005
    Posts
    300

    Re: Looking For Letters In Strings

    VB Code:
    1. If InStr(LCase(List1.List(I)), OldLetter) and InStr(LCase(List1.list(i)), OldLetter1 Then

    Would This Work ?
    Im Learning !!!!

  9. #9
    I'm about to be a PowerPoster! Joacim Andersson's Avatar
    Join Date
    Jan 1999
    Location
    Sweden
    Posts
    14,649

    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?

  10. #10

    Thread Starter
    Hyperactive Member
    Join Date
    Feb 2005
    Posts
    300

    Re: Looking For Letters In Strings

    Yeah for anything to get replaced the string must contain both the letters its looking for.
    Im Learning !!!!

  11. #11

    Thread Starter
    Hyperactive Member
    Join Date
    Feb 2005
    Posts
    300

    Re: Looking For Letters In Strings

    VB Code:
    1. Public Sub DoItX(OldLetter As String, OldLetter1 As String, NewLetter As String, NewLetter1 As String)
    2.  
    3. Dim I As Integer
    4. Dim NewWord As String
    5.  
    6. For I = 0 To List1.ListCount - 1
    7.     If InStr(LCase(List1.List(I)), OldLetter) & InStr(LCase(List1.List(I)), OldLetter1) Then
    8.         NewWord = List1.List(I)
    9.         NewWord = Replace(LCase(NewWord), OldLetter, NewLetter)
    10.         NewWord = Replace(LCase(NewWord), OldLetter1, NewLetter1)
    11.         List2.AddItem NewWord
    12.     End If
    13. Next
    14.  
    15. End Sub

    this works, a bit long winded but it works.
    Im Learning !!!!

  12. #12
    I'm about to be a PowerPoster! Joacim Andersson's Avatar
    Join Date
    Jan 1999
    Location
    Sweden
    Posts
    14,649

    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:
    1. If InStr(1, List1.List(I), OldLetter, vbTextCompare) > 0 And InStr(1, List1.List(I), OldLetter1) > 0 Then
    2.     NewWord = Replace(List1.List(I), OldLetter, NewLetter, , , vbTextCompare)
    3.     NewWord = Replace(NewWord, OldLetter1, NewLetter1, , , vbTextCompare)
    4.     List2.AddItem NewWord
    5. End If

  13. #13

    Thread Starter
    Hyperactive Member
    Join Date
    Feb 2005
    Posts
    300

    Re: Looking For Letters In Strings

    sweet works great. Thanks alot.
    Im Learning !!!!

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