简体   繁体   English

如何在vb.net中检查字符串是否包含特殊字符?

[英]How to check whether the string contains special characters or not in vb.net?

I want to check whether the input contains special characters(@"[~`!@#$%^&*()-+=|{}':;.,<>/?]") or not in vb.net? 我想检查输入是否在vb.net中包含特殊字符(@“ [〜`!@#$%^&*()-+ = | {}':;。,<> /?]”) ?

How can I check that in vb.net code? 如何在vb.net代码中进行检查?

If you want to check if any of the mentioned characters are contained in the string you can use the following function: 如果要检查字符串中是否包含任何上述字符,可以使用以下函数:

Function ContainsSpecialChars(s As String) As Boolean
    Return s.IndexOfAny("[~`!@#$%^&*()-+=|{}':;.,<>/?]".ToCharArray) <> -1
End Function

Or if you want to check if the string just contains letters, digits or whitespace, you can use the following function: 或者,如果要检查字符串是否仅包含字母,数字或空格,则可以使用以下功能:

Function ContainsSpecialChars(s As String) As Boolean
    Return s.Any(Function(c) Not (Char.IsLetterOrDigit(c) OrElse Char.IsWhiteSpace(c)))
End Function

If the string can only contain letters or digits(0-9) or white-spaces: 如果字符串只能包含字母或数字(0-9)或空格:

Dim noSpecialCharacters = text.
    All(Function(c) Char.IsLetterOrDigit(c) OrElse Char.IsWhiteSpace(c))

Dim containsSpecialCharacters = Not noSpecialCharacters 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM