Results 1 to 5 of 5

Thread: searching for strings with object.indexOf [resolved]

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    Sep 2000
    Posts
    770

    searching for strings with object.indexOf [resolved]

    The method .indexOf is nice and all but it wont find strings that are of different case. Lest say I have a huge string buffer and need to search for a value but want it to be case-insensitive. Is there a built in method of VB.NET to do this?

    E.g. Searching for the value 'red' should match values of 'Red', 'RED', rEd', within a string.
    Last edited by nkad; Jan 8th, 2005 at 12:15 AM.

  2. #2
    Frenzied Member <ABX's Avatar
    Join Date
    Jul 2002
    Location
    Canada eh...
    Posts
    1,622

    Re: searching for strings with object.indexOf

    VB Code:
    1. Dim strSearchString as String = "text"
    2. Dim strTextToSearch as String = "mY tExT"
    3.  
    4. 'Just use the ToLower method of System.String to return a lower case instance of the search string and source string.
    5.  
    6. Msgbox (strTextToSearch.ToLower.IndexOf(strSearchString.ToLower))

    I knew i should have included a case insensitve example.
    Tips:
    • Google is your friend! Search before posting!
    • Name your thread appropriately... "I Need Help" doesn't cut it!
    • Always post your code!!!! We can't read your mind!!! (well, at least most of us!)
    • Allways Include the Name and Line of the Exception (if one is occuring!)
    • If it is relevant state the version of Visual Studio/.Net Framwork you are using (2002/2003/2005)


    If you think I was helpful, rate my post
    IRC Contact: Rizon/xous ChakraNET/xous Freenode/xous

  3. #3
    Hyperactive Member fret's Avatar
    Join Date
    Sep 2004
    Posts
    472

    Re: searching for strings with object.indexOf

    this one?
    VB Code:
    1. If TextBox1.Text.ToLower.IndexOf("red") <> -1 Then
    2.             MessageBox.Show("found")
    3.         Else
    4.             MessageBox.Show("not found")
    5.         End If
    that method can search what ever the case of "red".

    EDIT: sorry ABX for the crosspost

  4. #4
    Frenzied Member <ABX's Avatar
    Join Date
    Jul 2002
    Location
    Canada eh...
    Posts
    1,622

    Re: searching for strings with object.indexOf

    Heres the case-insensitve version of the example in ur previous post:

    VB Code:
    1. Imports System.IO ' for StreamReader
    2.  
    3. '-----
    4.  
    5. Public Function SearchFile(FileName as String, SearchString as String) as Boolean
    6.  
    7. Dim sr as new StreamReader(FileName)
    8.  
    9. Dim strLines() as String = sr.ReadToEnd.ToLower.Split(vbcrlf)
    10.  
    11. sr.Close
    12. sr = nothing
    13.  
    14. Dim intIndex as Integer '
    15.  
    16. For Each s as String in strLines
    17.     intIndex = s.IndexOf(SearchString.ToLower)' IndexOf returns -1 if the string not found
    18.     If not intIndex = -1 Then
    19.        Return True
    20.     End If
    21. Next
    22.  
    23. End Function
    Tips:
    • Google is your friend! Search before posting!
    • Name your thread appropriately... "I Need Help" doesn't cut it!
    • Always post your code!!!! We can't read your mind!!! (well, at least most of us!)
    • Allways Include the Name and Line of the Exception (if one is occuring!)
    • If it is relevant state the version of Visual Studio/.Net Framwork you are using (2002/2003/2005)


    If you think I was helpful, rate my post
    IRC Contact: Rizon/xous ChakraNET/xous Freenode/xous

  5. #5

    Thread Starter
    Fanatic Member
    Join Date
    Sep 2000
    Posts
    770

    Re: searching for strings with object.indexOf [resolved]

    Thanks though, at first I thought that M$ got rid of InStr() but I dbl checked and its still there so thats what I used. Not sure how fast it is compared to indexOf.

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