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
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
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
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
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.