【问题标题】:Verifying that a string contains only letters in console application in c# [duplicate]在 c# 的控制台应用程序中验证字符串是否仅包含字母 [重复]
【发布时间】:2014-01-23 18:38:57
【问题描述】:

我需要检查我的字符串是否只包含字母而没有数字。请注意,我知道在 stackoverflow 上已经发布了很多内容,但没有一个对我有帮助,因为它不使用基本功能!

这是我的作业,但请帮忙,因为我无法解决问题。

我已经通过使用 Regex.IsMatch 做到了这一点,但老师告诉我,我只能使用 .net 库中的基本函数,并且严格禁止使用复杂的函数,如排序、搜索字符等。

我的代码

Console.Write("Enter your name: ");

name = Console.ReadLine();
if (Regex.IsMatch(name, "[^A-Za-z_ŠšČ莞ĆćĐđ]"))
{
    Console.WriteLine("No name entered!");
    Console.ReadLine();
}

我不知道如何以其他方式做到这一点。所以如果有任何人愿意帮助我或给我一个提示,我真的很高兴。

谢谢。

【问题讨论】:

  • @Yuriy,你在发这个之前有没有读过我的整篇文章?
  • 在 c/c++ 中是if ( !( (c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z') ) ) { /** not an ascii leter **/ }
  • 我在这里查看了答案,他们都使用了所谓的“非基本”函数或方法。当然,您的老师已经定义了“基本”的含义。你能在这里重现这个定义吗?否则,我认为没有人能够提供帮助。您是否可以遍历提交的所有字母?
  • @Cyber​​herbalist,如果你自己实现它,你只能使用 charIsLetter。否则,允许冒泡排序或下面写的 ASCII。
  • @sln 谢谢你,但我已经卖了一个问题。无论如何,感谢您的贡献!

标签: c# regex string console-application


【解决方案1】:

使用char.IsLetter 检查字符串中的所有字符是否都是字母。

if (name.All(char.IsLetter))
{
    //All characters are letters
}

同:

if (name.All(r => char.IsLetter(r)))

【讨论】:

  • 需要用基本功能来完成。现在允许使用 char.IsLetter
  • @fkr,我可以给你一个提示,遍历你的字符串的每个字符,检查字符的 ASCII 值是否在 48 到 57 之间,如果是,那么你的字符串中有一个数字.
  • 谢谢,我会试试这个。
  • 效果很好。谢谢:)
  • if (checkName[j] > 47 && checkName[j]
【解决方案2】:

循环字符并检查它们的 ascii 值:

public Boolean IsAllLetters(String name)
{
    for (Int32 i = 0; i < name.Length; i++)
    {
        if ((Int32)name[i] < 65 || ((Int32)name[i] > 90 && (Int32)name[i] < 97) || (Int32)name[i] > 122)
            return false;
    }
    return true;
}

我将提供另一种方法。我采用了一对扩展方法来实现这一点,让我可以灵活地查找任何字符组:

public static class StringEx
{
    public static Boolean ContainsOnly(this String theString, String characters)
    {
        return theString.TrimStart(characters.ToCharArray()).Length == 0;
    }

    public static Boolean ContainsOnly(this String theString, Char[] characters)
    {
        return theString.TrimStart(characters).Length == 0;
    }
}

【讨论】:

  • "我只能使用 .net 库中的基本功能,并严格禁止使用复杂功能,如排序、搜索字符等。" ---> 这意味着没有包含或 TrimStart 或任何其他非基本
  • Name[i]
  • @DavidKhaykin 谢谢,已编辑。凭记忆工作...
【解决方案3】:

不使用任何特殊功能:

    public void RecognizeOnlyLettersInString()
    {
        string name = "test the 123 thing";
        Console.WriteLine(name);

        string letters = "abcdefghijklmnopqrstuvwxyz";
        foreach (char c in name)
        {
            foreach(char letter in letters)
            {
                if (c == letter)
                {
                    break;
                }

                if (letters.IndexOf(letter) == letters.Count() - 1)
                {
                    Console.WriteLine("{0} is not a letter", c);
                }
            }
        }
    }

【讨论】:

  • 非常感谢,但我已经通过使用 Habib 编写的 ASCII 解决了这个问题。非常感谢您的麻烦!
【解决方案4】:

好吧,我看到一个答案已经被接受了,但是它使用了提问者说他​​不能使用的功能,所以既然我费心构建答案,我就将它放在这里以备不时之需得出结论。

Console.WriteLine("Enter your name: ");
string s = Console.ReadLine();
byte[] chars = Encoding.UTF8.GetBytes(s);
bool hasnonletters = false;
for (int x = 0; x < chars.Length; x++)
{
    Console.Write(".");
    int ascii = (int)chars[x];
    if (!((ascii > 64 && ascii < 91) || (ascii > 96 && ascii < 123)))
    {
        hasnonletters = true;
        break;
    }
}

if (hasnonletters)
{
    Console.WriteLine("{0} has Non-letters!", s);
}
else
{
    Console.WriteLine("{0} is all letters!", s);
}

Console.ReadKey();

【讨论】:

    猜你喜欢
    • 2010-11-13
    • 1970-01-01
    • 2012-09-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-07-16
    • 2014-06-21
    • 1970-01-01
    相关资源
    最近更新 更多