[英]Check a string if it only contains letters from the alphabet and not numbers or other stuff in C#?
I have a question. 我有个问题。 I'm researched it on the web and of course with the search function here on stackoverflow. 我已经在网络上对其进行了研究,当然还有在stackoverflow上使用了搜索功能。 I also found a few similiar question but they where either in another programming language or not exactly I want to do. 我还发现了一些类似的问题,但是它们在另一种编程语言中还是我不想要的地方。 So here is my question in detail: 所以这是我的详细问题:
I'm creating a simple console Hangman game (im a beginner). 我正在创建一个简单的控制台Hangman游戏(初学者)。 I'm doing this just on my own because I think I learn the most if I do it on my own. 我只是自己做这件事,因为如果我自己做的话,我认为我学得最多。 Anyway I ask the user for his word and save it into a variable. 无论如何,我都会向用户询问他的单词并将其保存到变量中。 I also make it Uppercase. 我也把它变成大写。 Now I want to check if this word only contains valid letters from the alphabet. 现在,我要检查此单词是否仅包含字母表中的有效字母。 Than I want to put this in a if condition so he will be ask again if he enters for example a number. 比我想把它设置为if条件,以便再次询问他是否输入数字。
But I have no idea how to do this? 但是我不知道该怎么做? I tried it with the tryparse method and contains but I can't come to a solution. 我用tryparse方法尝试过并包含它,但是我无法解决。 So I'm asking you kindly how to do this? 所以我想问您如何做到这一点?
My code: 我的代码:
class Hangman
{
private string enteredWord;
private char guessedLetter;
public void Start()
{
enteredWord = AskUserForWord();
Console.WriteLine(enteredWord);
Console.ReadLine();
}
private string AskUserForWord()
{
Console.WriteLine("Bitte gib ein Wort ein das es zu erraten gilt. Sag deinem Mitspieler er soll weg sehen: ");
string word = ToUpper(Console.ReadLine());
if ()
return word;
}
private string ToUpper(string word)
{
return word.ToUpper();
}
}
First reference system.linq: 首先参考system.linq:
using System.Linq;
then modify your AskUserForWord()
function: 然后修改您的AskUserForWord()
函数:
while (true)
{
Console.WriteLine("Bitte gib ein Wort ein das es zu erraten gilt. Sag deinem Mitspieler er soll weg sehen: ");
string word = Console.ReadLine().ToUpper();
if (word.All(c => char.IsLetter(c))
return word;
Console.WriteLine("Also bitte, nur Buchstaben sind hier erlaubt!");
}
This should do the job : 这应该做的工作:
bool result = input.All(Char.IsLetter);
and also a regex solution : 还有一个正则表达式解决方案:
Regex.IsMatch(theString, @"^[\p{L}]+$");
The Char.IsLetter is the better solution since it counts as a letter any language alphabets. Char.IsLetter是更好的解决方案,因为它将任何语言字母都视为字母。 This regex will also count them but still it looks more compact with IsLetter 这个正则表达式也将计算在内,但使用IsLetter看起来仍然更紧凑
You can use regular expressions. 您可以使用正则表达式。
For alphabets, 对于字母,
Regex.IsMatch(word, @"^[a-zA-Z]+$");
可以对此进行检查。
bool isLettersOnly = !word.Any(c => !char.IsLetter(c));
read up on Regex aka Regular expressions, they are good for describing exactly how you want your input to be, in your case it would be something like: 阅读正则表达式又称正则表达式,它们非常适合准确描述您希望输入的内容,在这种情况下,它类似于:
Regex(@"^[a-zA-Z]*");
this means only accept strings which consist of alphabetic characters. 这意味着仅接受由字母字符组成的字符串。
look at this example https://msdn.microsoft.com/en-us/library/3y21t6y4(v=vs.110).aspx to understand how to use it. 查看此示例https://msdn.microsoft.com/zh-cn/library/3y21t6y4(v=vs.110).aspx,以了解如何使用它。
You may also try with a Regular expression. 您也可以尝试使用正则表达式。
Match match = Regex.Match("Testing a valid input ", @"(?i)^[a-z]+");
if (match.Success)
{
//validation passed
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.