|
-
Jan 7th, 2005, 03:52 PM
#1
Thread Starter
Fanatic Member
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.
-
Jan 7th, 2005, 09:12 PM
#2
Re: searching for strings with object.indexOf
VB Code:
Dim strSearchString as String = "text"
Dim strTextToSearch as String = "mY tExT"
'Just use the ToLower method of System.String to return a lower case instance of the search string and source string.
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
-
Jan 7th, 2005, 09:13 PM
#3
Hyperactive Member
Re: searching for strings with object.indexOf
this one?
VB Code:
If TextBox1.Text.ToLower.IndexOf("red") <> -1 Then
MessageBox.Show("found")
Else
MessageBox.Show("not found")
End If
that method can search what ever the case of "red". 
EDIT: sorry ABX for the crosspost
-
Jan 7th, 2005, 09:15 PM
#4
Re: searching for strings with object.indexOf
Heres the case-insensitve version of the example in ur previous post:
VB Code:
Imports System.IO ' for StreamReader
'-----
Public Function SearchFile(FileName as String, SearchString as String) as Boolean
Dim sr as new StreamReader(FileName)
Dim strLines() as String = sr.ReadToEnd.ToLower.Split(vbcrlf)
sr.Close
sr = nothing
Dim intIndex as Integer '
For Each s as String in strLines
intIndex = s.IndexOf(SearchString.ToLower)' IndexOf returns -1 if the string not found
If not intIndex = -1 Then
Return True
End If
Next
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
-
Jan 8th, 2005, 12:15 AM
#5
Thread Starter
Fanatic Member
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|