Guys,
How can I see if a string containing a single name, is contained within a longer string containing lots of names ?
Bob
Printable View
Guys,
How can I see if a string containing a single name, is contained within a longer string containing lots of names ?
Bob
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.
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
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
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:
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. :sick: Any idea from some clever boy?? :rolleyes: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
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
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. :eek: :confused:
Quote:
Originally Posted by Libero
VB.Net Code:
Dim text As String = "this is my string" If text.Contains("my") Then MessageBox.Show("yeah!") End If
:D
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.Quote:
Originally Posted by Libero