![](/img/trans.png)
[英]How do I capitalized specific two letter word in a string in addition to the first letter of any word?
[英]Validating user only enters a one word answer and that the first letter is capitalized
我目前正在自学C#,并且对它掌握得相当不错,但是我迷失在如何验证用户仅输入一个单词答案以及是否大写的方式上,否则我想给他们另一个机会尝试。
这是我到目前为止所拥有的:
static void Main(string[] args)
{
//assigned variable
string userInput;
//intializing empty string
string answerInput = string.Empty;
//Creating loop
while ((answerInput == string.Empty) || (answerInput == "-1"))
{
//This is asking the question to the user
Console.WriteLine("Enter your favorite animal: ");
//this is storing users input
userInput = Console.ReadLine();
//using function to validate response
answerInput = letterFunc(userInput);
}
}
//Creating function to only allow letters and making sure it's not left blank.
private static string letterFunc (string validate)
{
//intializing empty string
string returnString = string.Empty;
//validating it is not left empty
if(validate.Length > 0)
{
//iterating through the passed string
foreach(char c in validate)
{
//using the asciitable to validate they only use A-Z, a-z, and space
if ((((Convert.ToInt32(c)) > 64) && ((Convert.ToInt32(c)) < 91)) || (((Convert.ToInt32(c)) > 96) && ((Convert.ToInt32(c)) < 123)) || (Convert.ToInt32(c) == 32))
{
//appensing sanitized character to return string
returnString += c;
}
else
{
//If they try to enter a number this will warn them
Console.WriteLine("Invalid input. Use letters only.");
}
}
}
else
{
//If user enters a blank input, this will warn them
Console.WriteLine("You cannot enter a blank response.");
}
//returning string
return returnString;
}
我想知道是否有可能在我创建的用于验证他们仅使用字母的函数中进行操作,并且通过详细说明它不会为空。 谢谢。
正则表达式并不难。 问题是有时候您想实现更复杂的目标,但事实并非如此:
private static string letterFunc (string validate)
{
return new Regex("^[A-Z][a-z]*$").IsMatch(validate) ?
validate :
string.Empty;
}
解释表达式:
^
-锚点:仅在文本以表达式开头时匹配
[AZ]
-正确的一个字符,从A到Z
[az]*
-零个或多个字符,从a到z
$
-锚点:仅当文本以表达式结尾时才匹配
通过使用两个锚点,我们希望全文匹配表达式,而不是表达式的一部分。
如果要在第一个字母(例如, CaT
或DoG
)之后使用大写字母,可以将其更改为:
^[AZ][a-zA-Z]*$
使用Regex玩: https : //regex101.com/r/zwLO6I/2
正则表达式将是执行此操作的标准方法,但是查看您的代码,我认为您还没有准备好使用它们。 顺便说一句,这不是侮辱-每个人在某个时候都是初学者!
每当您遇到这样的问题时,首先要确保所有要求都定义明确且具体:
单字答案:在代码中,您已将其定义为“仅包含字母和空格的答案”。 这可能并不理想,因为它阻止人们输入像dik-dik这样的答案作为他们最喜欢的动物。 但是,让我们暂时坚持下去。
大写答案:我们将其定义为“第一个字符为大写字母的答案”。
因此,将这两个要求放在一起,我们正在尝试验证答案是否以大写字母开头并且仅包含字母和空格。
在编码时,请查看您使用的语言和框架,以查看是否有方便的方法可以为您提供帮助。 .NET有很多此类功能。 我们知道我们将必须检查String
的各个字符,并且字符串是由Char
的,因此让我们搜索“ c#char type”。 查看MSDN页面上的System.Char ,我们可以看到一些可以帮助我们的方法:
Char.IsWhiteSpace
测试字符是否为“空白”(空格,制表符,换行符) Char.IsLetter
测试字符是否为字母。 Char.IsUpper
测试字符是否为大写字母。 因此,让我们再次看一下我们的需求并一次实现一个需求:“以大写字母开始,仅包含字母和空格”。
让我们将其称为用户输入字符串answer
。 我们可以检查第一个字母是否为大写字母(请注意,我们还要确保它具有第一个字母):
bool isCapitalized = answer.Length > 0 && Char.IsUpper( answer[0] );
我们可以检查它是否仅包含字母和空格,如下所示:
bool containsOnlyLettersAndSpaces = true;
foreach( char c in answer )
{
if( !Char.IsLetter( c ) && !Char.IsWhiteSpace( c ) )
{
containsOnlyLettersAndSpaces = false;
break;
}
}
containsOnlyLettersAndSpaces
从true开始。 然后,我们查看字符串中的每个字符。 如果找到的字符不是字母也不是空格,则将containsOnlyLettersAndSpaces
设置为false。 另外,如果发现无效字符,请停止检查。 我们知道答案现在无效,没有理由检查其余内容!
现在,我们可以结合两个验证来返回答案:
return isCapitalized && containsOnlyLettersAndSpaces;
这是整个方法:
private bool IsValidAnimal( string answer )
{
bool isCapitalized = answer.Length > 0 && Char.IsUpper( answer[0] );
bool containsOnlyLettersAndSpaces = true;
foreach( char c in answer )
{
if( !Char.IsLetter( c ) && !Char.IsWhiteSpace( c ) )
{
containsOnlyLettersAndSpaces = false;
break;
}
}
return isCapitalized && containsOnlyLettersAndSpaces;
}
祝您学习C#一切顺利,我希望这可以帮助您考虑如何编码!
我想到了。 感谢大家的帮助。
string answer;
while (true)
{
Console.WriteLine("Enter your favorite animal:");
answer = Console.ReadLine();
if (new Regex("^[A-Z][a-z]*$").IsMatch(answer))
{
Console.WriteLine("You like {0}s. Cool!", answer);
Console.ReadKey();
break;
}
else
{
Console.WriteLine("'{0}' is not a valid answer.", answer);
Console.WriteLine();
Console.WriteLine("Make sure:");
Console.WriteLine("You are entering a one word answer.");
Console.WriteLine("You are only using letters.");
Console.WriteLine("You are capitalizing the first letter of the word.");
Console.WriteLine();
Console.WriteLine("Try again:");
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.