|
-
Feb 23rd, 2006, 05:45 AM
#1
Thread Starter
Fanatic Member
Compare strings
Guys,
How can I see if a string containing a single name, is contained within a longer string containing lots of names ?
Bob
"I dislike 7 am. If 7 am were a person, I would punch 7 am in the biscuits." - Paul Ryan, DailyRamblings
-
Feb 23rd, 2006, 05:52 AM
#2
Re: Compare strings
In .NET 2.0 the String class has a Contains method. In earlier versions you make use of the fact that String.IndexOf returns -1 if the specified substring is not contained in the string.
-
Feb 23rd, 2006, 06:03 AM
#3
Thread Starter
Fanatic Member
Re: Compare strings
Thanks John. This works fine as I'm still on 1.1.
VB Code:
If cb_rfiissueto.Text.IndexOf(tb_distribution.Text) < 0 Then
'Not in distrib
RaiseEvent MailOKChange(pb_MAILYES.Visible, cb_rfiissueto.Text, pb_MAILYES.Visible)
Else
'In distrib
RaiseEvent MailOKChange(pb_MAILYES.Visible, cb_rfiissueto.Text, False)
End If
Bob
"I dislike 7 am. If 7 am were a person, I would punch 7 am in the biscuits." - Paul Ryan, DailyRamblings
-
Feb 23rd, 2006, 06:47 AM
#4
Re: Compare strings
You can also use Regex, with the .IsMatch method with the name you want to find, it will return true if found....
VB Code:
Dim MyString As String = "John Bob MyName Frank"
Dim Regex As New System.Text.RegularExpressions.Regex(" MyName ")
If Regex.IsMatch(MyString) = True Then
MessageBox.Show("Found it!")
Else
MessageBox.Show("Didnt Find it!")
End If
-
Feb 7th, 2008, 01:34 PM
#5
Hyperactive Member
Re: Compare strings
Hiya! I was searching for function to find and replace string and found this thread. Theere is one interesting thing with this way of findning strings. First, lets run this code:
Code:
Dim text As String
text = "This is my string"
If text.Contains("") Then
MsgBox("Found")
Else
MsgBox("Not found")
End If
Same thing with this:
Code:
Dim MyString As String = "John Bob MyName Frank"
Dim Regex As New System.Text.RegularExpressions.Regex("")
If Regex.IsMatch(MyString) = True Then
MessageBox.Show("Found it!")
Else
MessageBox.Show("Didnt Find it!")
End If
This code will return Found. Is that a proper way to code? Im in deep need of a way to easy compare 2 strings. And i cant use this one since it thinks an empty string is the same as any string. Any idea from some clever boy??
Last edited by Libero; Feb 7th, 2008 at 01:37 PM.
-
Feb 7th, 2008, 01:49 PM
#6
Re: Compare strings
I dont see why you'd want to see if a string contains an empty string using IndexOf/Contains...that just sounds strange.
If you want to know if a string equals an empty string why not do a normal comparison?
VB.Net Code:
If text = String.Empty Then
End If
or
VB.Net Code:
If text.Equals(String.Empty) Then
End If
-
Feb 7th, 2008, 03:16 PM
#7
-
Feb 7th, 2008, 03:18 PM
#8
Re: Compare strings
 Originally Posted by Libero
No, i just want to know if "my" can be found in "this is my string"
VB.Net Code:
Dim text As String = "this is my string"
If text.Contains("my") Then
MessageBox.Show("yeah!")
End If
-
Feb 7th, 2008, 04:56 PM
#9
Re: Compare strings
 Originally Posted by Libero
No, i just want to know if "my" can be found in "this is my string". I dont want my code to say that an empty or a string with space " " is the same as "this is my string". Hehe this sounds crazy, i know. 
By definition every string contains the empty string as a substring. If you don't want to look for an empty string then don't. Simply test your substring first to see if it's an empty string and if it is then don't even search for it.
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
|