C# 字符串需要包含2个单词
我的一个视图上有一个文本框,该文本框不应接受任何超过2个单词或少于2个单词的内容。这个文本框需要两个单词 基本上,这个文本框接受一个人的名字和姓氏。我不希望人们只进入其中一个 有没有办法检查两个单词和另一个C# 字符串需要包含2个单词,c#,asp.net-mvc-5,C#,Asp.net Mvc 5,我的一个视图上有一个文本框,该文本框不应接受任何超过2个单词或少于2个单词的内容。这个文本框需要两个单词 基本上,这个文本框接受一个人的名字和姓氏。我不希望人们只进入其中一个 有没有办法检查两个单词和另一个空格字符之间是否有空格字符,以及第二个单词后面是否有任何字母,数字,等等?我认为,如果用户不小心在第二个单词后面加了一个额外的空格,那应该没问题,因为仍然只有两个单词 例如: /* the _ character means space */ John /* no
空格
字符之间是否有空格
字符,以及第二个单词后面是否有任何字母
,数字
,等等?我认为,如果用户不小心在第二个单词后面加了一个额外的空格,那应该没问题,因为仍然只有两个单词
例如:
/* the _ character means space */
John /* not accepted */
John_ /* not accepted */
John_Smith_a /* not accepted */
John Smith_ /* accepted */
非常感谢您的帮助。像这样使用它
string s="John_Smith_a"
if (s.Trim().Split(new char[] { ' ' }).Length > 1)
{
}
您可以使用多种方法来解决这个问题,我将回顾一些 使用
String.Split()
方法
您可以使用该方法根据分隔符将字符串分解为各个组件。在这种情况下,可以使用空格作为分隔符来获取单个单词:
// Get your words, removing any empty entries along the way
var words = YourTextBox.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
// Determine how many words you have here
if(words.Length != 2)
{
// Tell the user they made a horrible mistake not typing two words here
}
使用正则表达式
此外,您可以尝试使用以下方法通过正则表达式解决此问题:
该表达式本身可能看起来有点吓人,但它可以分解如下:
^ # This matches the start of your string
(\s+)? # This optionally allows for a single series of one or more whitespace characters
\w+ # This allows for one or more "word" characters that make up your first word
\s+ # Again you allow for a series of whitespace characters, you can drop the + if you just want one
\w+ # Here's your second word, nothing new here
(\s+)? # Finally allow for some trailing spaces (up to you if you want them)
“word”字符\w
是正则表达式中的一种特殊字符,可以表示数字、字母或下划线,它与[A-zA-Z0-9\]
等效
使用MVC的正则表达式属性利用正则表达式
最后,由于您使用的是MVC,您可以利用模型本身的属性:
[RegularExpression(@"^(\s+)?\w+\s+\w+(\s+)?", ErrorMessage = "Exactly two words are required.")]
public string YourProperty { get; set; }
这将允许您在控制器操作中调用ModelState.IsValid
,查看您的模型是否有任何错误:
// This will check your validation attributes like the one mentioned above
if(!ModelState.IsValid)
{
// You probably have some errors, like not exactly two words
}
最干净的方法是将正则表达式与IsMatch
方法一起使用,如下所示:
Regex.IsMatch("One Two", @"^\w+\s\w+\s?$")
如果输入匹配,则返回true
Match m = Regex.Match(this.yourTextBox.Text, @"[^\w\s\w$]", String.Empty);
if (m.Success)
//do something
else
//do something else
我对正则表达式的了解非常有限,我相信这会解决您的问题。试试这个
if (str.Split(' ').Length == 2)
{
//Do Something
}
str是保存字符串以进行比较的变量这里的标记意味着MVC,因此我建议使用
类:
public class YourModel
{
[RegularExpression(@"[^\w\s\w$]", ErrorMessage = "You must have exactly two words separated by a space.")]
public string YourProperty { get; set; }
}
一个表达式来匹配像Cpl\3 John Smith
或John Smith先生这样的东西怎么样?我有@“^(\s+)[A-Za-z.-]+\s\w+\s\w+(\s+)?$”
您是想在您以前的案例之外还是作为一个完全独立的集合来允许这些案例?因为从技术上讲,这些词包括三个词。是的,除了我以前的案例。3是最大值和最小值。如果您希望只允许三组单词,您可以在其中附加另一组\w+
,但是如果您希望允许斜杠和小数,则需要[\w.\\]+
像@“^(\s+)?[\w.\\]+\s+\w+\s+\w+\s+(\s+)$”
。你可以。
public class YourModel
{
[RegularExpression(@"[^\w\s\w$]", ErrorMessage = "You must have exactly two words separated by a space.")]
public string YourProperty { get; set; }
}