在C#中验证字符串变量 [英] validating string variable in c#
本文介绍了在C#中验证字符串变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
嗨
我有一个如下所示的字符串变量.
字符串referenceNumber;
我想验证referenceNumber,因为它应该只包含字母,数字和下划线(_).
例如:-ghk234_1
除字母,数字和下划线(_)之外,该字符不得包含其他任何字符.
问候,
Nagaraju
Hi
I have a string variable as below.
string referenceNumber;
i want to validate referenceNumber as it should contain only alphabets,numbers and underscore(_).
ex:- ghk234_1
it should not allow any other except alphabets,numeric,and underscore(_).
Regards,
Nagaraju
推荐答案
查看正则表达式:System.Text.RegularExpressions.Regex.
Look into regular expressions: System.Text.RegularExpressions.Regex.
String testString = "ghk234_1";
String regExpString = "[a-zA-Z0-9_]*";
Regex regex = new Regex(regExpString);
Match match = regex.Match(testString);
if(match.Success)
{
//String was OK
}
else
{
//String was not OK
}
最好的问候,
Best Regards,
使用
Use regular expressions. It''s the most efficient way to analyze text information.
这篇关于在C#中验证字符串变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文