Results 1 to 6 of 6

Thread: What is the best way to find a String without space ?

  1. #1

    Thread Starter
    Banned
    Join Date
    May 2021
    Posts
    180

    What is the best way to find a String without space ?

    Hello

    I am trying to look out for a specific string which exists WITHOUT SPACE but does not match ?

    "theQuickBrownfoxjumpedoverthelazydog"
    How can i get only Brown from above sentence
    Code:
    Dim sentence As String = "theQuickBrownfoxjumpedoverthelazydog"
    TextBox1.Text = sentence
    
    'Now when i use below
    If TextBox1.Contains("Brown")  Then ' it is not Matching or not able to detect
    So what would be simple way to get the desired output ie Brown

    Thanks
    SamD
    173

  2. #2
    Hyperactive Member
    Join Date
    Jul 2022
    Location
    Buford, Ga USA
    Posts
    507

    Re: What is the best way to find a String without space ?

    You need to use the .Text property of the textbox (This is a .net framework 4.8 project)

    Code:
           Dim sentence As String = "theQuickBrownfoxjumpedoverthelazydog"
           TextBox1.Text = sentence
    
           'If sentence. Contains("Brown") Then
           '    MsgBox("found it", MsgBoxStyle.Information, "variable")
           'Else
           '    MsgBox("Nope", MsgBoxStyle.Information, "variable")
           'End If
    
           If TextBox1.Text.Contains("Brown") Then
               MsgBox("found it", MsgBoxStyle.Information, "textbox")
           Else
               MsgBox("Nope", MsgBoxStyle.Information, "textbox")
           End If
    Attached Images Attached Images  

  3. #3
    Super Moderator dday9's Avatar
    Join Date
    Mar 2011
    Location
    South Louisiana
    Posts
    12,089

    Re: What is the best way to find a String without space ?

    There are a few ways.

    One way is to use String.IndexOf (documentation) to check if the index of of the first occurrence of a specific value. This method returns -1 if the value is not found.

    Another way is to use String.Contains (documentation) which jdelano has already provided you an example of.

    Yet another way is to use Regular Expressions (aka RegEx). RegEx applies a match pattern to a String an returns a series of matched values. RegEx is extremely flexible and is probably overkill in this situation, but I wanted to bring it up nonetheless.

    Here are examples of using each:
    Code:
    Private Function IndexOfMatch(valueToSearch As String, valueSearchingFor As String) As Boolean
        Dim index = valueToSearch.IndexOf(valueSearchingFor)
        Dim isMatch = index > -1
        Return isMatch
    End Function
    
    Private Function ContainsMatch(valueToSearch As String, valueSearchingFor As String) As Boolean
        Dim isMatch = valueToSearch.Contains(valueSearchingFor)
        Return isMatch
    End Function
    
    Private Function RegExMatch(valueToSearch As String, valueSearchingFor As String) As Boolean
        Dim isMatch = RegEx.IsMatch(valueToSearch, valueSearchingFor)
        Return isMatch
    End Function
    Fiddle: https://dotnetfiddle.net/YGXO7f
    "Code is like humor. When you have to explain it, it is bad." - Cory House
    VbLessons | Code Tags | Sword of Fury - Jameram

  4. #4
    PowerPoster Zvoni's Avatar
    Join Date
    Sep 2012
    Location
    To the moon and then left
    Posts
    5,033

    Re: What is the best way to find a String without space ?

    Didn‘t we have that some weeks ago, where I presented a Regex solution?
    Last edited by Zvoni; Tomorrow at 31:69 PM.
    ----------------------------------------------------------------------------------------

    One System to rule them all, One Code to find them,
    One IDE to bring them all, and to the Framework bind them,
    in the Land of Redmond, where the Windows lie
    ---------------------------------------------------------------------------------
    People call me crazy because i'm jumping out of perfectly fine airplanes.
    ---------------------------------------------------------------------------------
    Code is like a joke: If you have to explain it, it's bad

  5. #5

    Thread Starter
    Banned
    Join Date
    May 2021
    Posts
    180

    Re: What is the best way to find a String without space ?

    jdelano
    You need to use the .Text property of the textbox
    Oh Yes Need to use .Text property of textbox before .Contains
    ie
    Code:
    TextBox1.Text.Contains("Brown")
    As this Slipped out of my mind because many times
    I define
    Dim ContentOftxtBx1 As String = Textbox1.Text
    and therefore lead to
    ContentOftxtBx1.Contains which ultimately lead to confusion

    Zvoni
    this is not related to regex for now

    dday9
    Thanks very much for more options. Wonderful Functions. Really impressed
    BTW How will your functions help in what scenario

    In Simplicity What would be better Option to use .Text.Contains or the functions you created.

    Thanks Everyone
    SamD
    174

  6. #6
    Super Moderator dday9's Avatar
    Join Date
    Mar 2011
    Location
    South Louisiana
    Posts
    12,089

    Re: What is the best way to find a String without space ?

    Quote Originally Posted by SamDsouza View Post
    dday9
    Thanks very much for more options. Wonderful Functions. Really impressed
    BTW How will your functions help in what scenario
    You would simply pass the string you want to search in and value you want to search for, e.g.
    Code:
    Dim isMatch = ContainsMatch(TextBox1.Text, "Brown")
    "Code is like humor. When you have to explain it, it is bad." - Cory House
    VbLessons | Code Tags | Sword of Fury - Jameram

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