Results 1 to 9 of 9

Thread: Compare strings

  1. #1

    Thread Starter
    Fanatic Member staticbob's Avatar
    Join Date
    Jan 2005
    Location
    Manchestershire, UK Cabbage: I do
    Posts
    619

    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

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,222

    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.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3

    Thread Starter
    Fanatic Member staticbob's Avatar
    Join Date
    Jan 2005
    Location
    Manchestershire, UK Cabbage: I do
    Posts
    619

    Re: Compare strings

    Thanks John. This works fine as I'm still on 1.1.

    VB Code:
    1. If cb_rfiissueto.Text.IndexOf(tb_distribution.Text) < 0 Then
    2.                     'Not in distrib
    3.                     RaiseEvent MailOKChange(pb_MAILYES.Visible, cb_rfiissueto.Text, pb_MAILYES.Visible)
    4.                 Else
    5.                     'In distrib
    6.                     RaiseEvent MailOKChange(pb_MAILYES.Visible, cb_rfiissueto.Text, False)
    7.                 End If

    Bob
    "I dislike 7 am. If 7 am were a person, I would punch 7 am in the biscuits." - Paul Ryan, DailyRamblings

  4. #4
    PowerPoster
    Join Date
    Aug 2005
    Location
    College Station, TX
    Posts
    4,521

    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:
    1. Dim MyString As String = "John Bob MyName Frank"
    2.         Dim Regex As New System.Text.RegularExpressions.Regex(" MyName ")
    3.         If Regex.IsMatch(MyString) = True Then
    4.             MessageBox.Show("Found it!")
    5.         Else
    6.             MessageBox.Show("Didnt Find it!")
    7.         End If

  5. #5
    Hyperactive Member Libero's Avatar
    Join Date
    Jun 2000
    Location
    Swedish viking
    Posts
    460

    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.

  6. #6
    Raging swede Atheist's Avatar
    Join Date
    Aug 2005
    Location
    Sweden
    Posts
    8,018

    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:
    1. If text = String.Empty Then
    2.  
    3. End If

    or

    VB.Net Code:
    1. If text.Equals(String.Empty) Then
    2.  
    3. End If
    Rate posts that helped you. I do not reply to PM's with coding questions.
    How to Get Your Questions Answered
    Current project: tunaOS
    Me on.. BitBucket, Google Code, Github (pretty empty)

  7. #7
    Hyperactive Member Libero's Avatar
    Join Date
    Jun 2000
    Location
    Swedish viking
    Posts
    460

    Re: Compare strings

    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.

  8. #8
    Raging swede Atheist's Avatar
    Join Date
    Aug 2005
    Location
    Sweden
    Posts
    8,018

    Re: Compare strings

    Quote Originally Posted by Libero
    No, i just want to know if "my" can be found in "this is my string"
    VB.Net Code:
    1. Dim text As String = "this is my string"
    2. If text.Contains("my") Then
    3.     MessageBox.Show("yeah!")
    4. End If

    Rate posts that helped you. I do not reply to PM's with coding questions.
    How to Get Your Questions Answered
    Current project: tunaOS
    Me on.. BitBucket, Google Code, Github (pretty empty)

  9. #9
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,222

    Re: Compare strings

    Quote 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.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

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