验证字符串包含在C#中唯一的字母 [英] Verifying that a string contains only letters in C#
本文介绍了验证字符串包含在C#中唯一的字母的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个输入字符串,我想验证它包含:
I have an input string and I want to verify that it contains:
- 只有字母或
- 只有字母和数字,或
- 只有字母,数字或下划线
要澄清一下,我在code 3种不同的情况下,每个要求不同的验证。是什么在C#中实现这一目标的最简单的方法?
To clarify, I have 3 different cases in the code, each calling for different validation. What's the simplest way to achieve this in C#?
推荐答案
只有字母:
Regex.IsMatch(input, @"^[a-zA-Z]+$");
只有字母和数字:
Only letters and numbers:
Regex.IsMatch(input, @"^[a-zA-Z0-9]+$");
只有字母,数字和下划线:
Only letters, numbers and underscore:
Regex.IsMatch(input, @"^[a-zA-Z0-9_]+$");
这篇关于验证字符串包含在C#中唯一的字母的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文