简体   繁体   English

递归检查字符串中的所有字母是否都是大写

[英]Check if all the letters in a string are capital recursively

I have to check if all the letters are capital letters in recursion, and i dont know why this isnt working:我必须检查递归中的所有字母是否都是大写字母,我不知道为什么这不起作用:

public static bool IsCapital(string str)
    {
        if (str.Length == 1)
            return int.Parse(str[0].ToString()) > 65 && int.Parse(str[0].ToString()) < 90;
        return IsCapital(str.Substring(1)) && int.Parse(str[0].ToString()) > 65 && int.Parse(str[0].ToString()) < 90;
    }

It crashes and says: "Unhandled exception: System.FormatException: Input string was not in a correct format."它崩溃并说:“未处理的异常:System.FormatException:输入字符串的格式不正确。”

Console.WriteLine(IsCapital("abc"));

thanks.谢谢。

You're attempting to parse a char as an int , instead of casting it to an int .您正在尝试将char解析为int ,而不是将其转换为int

What you're doing is taking a letter, such as A , and parsing that as an int.您正在做的是获取一个字母,例如A ,并将其解析为一个 int。 A isn't a number in any way, and therefore the parse fails. A 无论如何都不是数字,因此解析失败。

What you'll want to do is explicitly cast the char as an int to get the ASCII value you're looking for:您要做的是将 char 显式转换为 int 以获得您要查找的 ASCII 值:

if (str.Length == 1)
{
    return ((int)str[0]) > 65 
        && ((int)str[0]) < 90;
}
return IsCapital(str.Substring(1)) 
    && ((int)str[0]) > 65 
    && ((int)str[0]) < 90;

I assume what you're trying to do with int.Parse(str[0].ToString()) is to get the ASCII value.我假设您尝试使用int.Parse(str[0].ToString())来获取 ASCII 值。

What you need to use instead is this (int)str[0]你需要使用的是这个(int)str[0]

A parse will try to translate the string into a number, so a string that has a value of '412' will get parsed into an int of value 412.解析将尝试将字符串转换为数字,因此值为“412”的字符串将被解析为值为 412 的整数。

I have to check if all the letters are capital letters in recursion我必须检查是否所有字母都是递归的大写字母

public static bool IsCapital(string str)
{
    if (String.IsNullOrEmpty(str)) return false;
    if (str.Length == 1 &&  char.IsUpper(str[0])) return true;
    return char.IsUpper(str[0]) ? IsCapital(str.Substring(1)) :false;
}

To solely address the exception, just don't parse strings.要单独解决异常,请不要解析字符串。 You can directly compare a char to any ushort value.您可以直接将char与任何ushort值进行比较。

In other words, this is a valid check (without string parsing)换句话说,这是一个有效的检查(没有字符串解析)

str[0] > 65

AsciiTable.com should show you why the checks you have will fail on the edges. AsciiTable.com应该向您展示为什么您的检查会在边缘处失败。

Also consider...还要考虑...

  • Characters that aren't letters.不是字母的字符。
  • IsCapital(null)

Finally, something that might make this easier (assuming non-letters get bypassed) is to create a method along the lines of bool IsNotLowerCase(char c) .最后,可能使这更容易(假设绕过非字母)的方法是创建一个bool IsNotLowerCase(char c)的方法。

Note -- these are all assuming ASCII, as evident by my link.注意——这些都假设为 ASCII,正如我的链接所证明的那样。

If you must support full Unicode, hopefully you can use the methods of char .如果你必须支持完整的 Unicode,希望你可以使用char的方法

I thinks, that number comparison will not work.我认为,这种数字比较是行不通的。

First it will return false for string like this "ABC123" even if all letters are capitals.首先,即使所有字母都是大写,它也会为像“ABC123”这样的字符串返回 false。 And second there are many national characters that does not fall into range 65..90 even if they are capitals.其次,有许多国家字符不属于 65..90 范围,即使它们是大写字母。 You should (if you can) use some Char method http://msdn.microsoft.com/en-us/library/d1x97616.aspx你应该(如果可以的话)使用一些 Char 方法http://msdn.microsoft.com/en-us/library/d1x97616.aspx

While there are many ways to skin this cat, I prefer to wrap such code into reusable extension methods that make it trivial to do going forward.虽然有很多方法可以给这只猫换皮,但我更喜欢将此类代码包装到可重用的扩展方法中,这样可以让以后的工作变得微不足道。 When using extension methods, you can also avoid RegEx as it is slower than a direct character check.使用扩展方法时,您还可以避免使用 RegEx,因为它比直接字符检查慢。 I like using the extensions in the Extensions.cs NuGet package.我喜欢使用 Extensions.cs NuGet 包中的扩展。 It makes this check as simple as:它使此检查变得如此简单:

  1. Add the [https://www.nuget.org/packages/Extensions.cs][1] package to your project.将 [https://www.nuget.org/packages/Extensions.cs][1] 包添加到您的项目中。
  2. Add " using Extensions; " to the top of your code.将“ using Extensions; ”添加到代码的顶部。
  3. "smith".IsUpper() will return False whereas "SMITH".IsUpper() will return True. "smith".IsUpper()将返回 False,而"SMITH".IsUpper()将返回 True。 4. Every other check in the rest of the code is simply MyString.IsUpper() . 4. 其余代码中的所有其他检查都只是MyString.IsUpper()

Your example code would become as simple as:您的示例代码将变得如此简单:

using Extensions;

//Console.WriteLine(IsCapital("abc"));
Console.WriteLine("abc".IsUpper());
public static bool IsCapital(string str)
{
 return !Regex.IsMatch(str, "[a..z]");
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM