Ola,
I want a textbox to be checked for specific characters. I can use:
How can I shorten this?Code:with textbox1.text
If .contains("a") or .contains("b") or .contains("c")...etc... then...
Printable View
Ola,
I want a textbox to be checked for specific characters. I can use:
How can I shorten this?Code:with textbox1.text
If .contains("a") or .contains("b") or .contains("c")...etc... then...
Code:If text.IndexOfAny(New Char() {"a"c, "b"c, "c"c}) > -1 Then
You can use the String.IndexOf (Any) to give you the loction of any of those characters and bail. Example here.
Thank you both. Will have a look at it.
Had to figure out why it kept saying that there was an illegal character although it wasn't.
"If text.IndexOfAny(New Char() {"a"c, "b"c, "c"c}) > -1 Then" means something like: When not one of the characters found in the index of the text".
If someone knows how to formulate it better, please let me know.
+REP btw. :thumb:
I can only guess that you want to run the If when you DO NOT have a value that matches, so just change it.
IndexOfAny.... = -1
I figured that already m8. "issue" was solved already. Thanks again.
These are just some examples:
Code:Dim text = "the quick brown fox!"
Dim letters = "abc".ToCharArray()
If Not text.ContainsAny("a"c, "b"c, "c"c) Then
End If
'//or
If Not text.ContainsAny(letters) Then
End If
'//or
If Not text.IndexOfAny(letters) > -1 Then
End If
Public Module StringExtensions
''' <summary>
''' Returns a value indicating whether any of the specified <see cref="System.Char" />
''' objects occur within this string.
''' </summary>
''' <param name="value">The value whose characters to check.</param>
''' <param name="chars">The <see cref="System.Char" /> objects to seek.</param>
<Runtime.CompilerServices.Extension()> _
Public Function ContainsAny(ByVal value As String, _
ByVal ParamArray chars() As Char) As Boolean
Return value.IndexOfAny(chars) > -1
End Function
End Module