2

すべての文字が再帰的に大文字であるかどうかを確認する必要がありますが、これが機能しない理由がわかりません。

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;
    }

クラッシュして、「未処理の例外:System.FormatException:入力文字列が正しい形式ではありませんでした。」と表示されます。

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

ありがとう。

4

7 に答える 7

1

例外に対処するためだけに、文字列を解析しないでください。charaを任意の値と直接比較できushortます。

言い換えれば、これは有効なチェックです(文字列の解析なし)

str[0] > 65

AsciiTable.comは、あなたが持っているチェックが端で失敗する理由をあなたに示すはずです。

また、考慮してください...

  • 文字ではない文字。
  • IsCapital(null)

最後に、これを簡単にする可能性のあるもの(非文字がバイパスされると仮定)は、の線に沿ってメソッドを作成することですbool IsNotLowerCase(char c)

注-私のリンクから明らかなように、これらはすべてASCIIを想定しています。

完全なUnicodeをサポートする必要がある場合は、のメソッドをchar使用できることを願っています。

于 2012-10-18T17:21:09.143 に答える
1

をにキャストするのではなく、charとして解析しようとしています。intint

あなたがしているのは、などの文字を取り、Aそれをintとして解析することです。Aは決して数値ではないため、解析は失敗します。

実行したいのは、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;
于 2012-10-18T17:02:05.643 に答える
1

あなたがやろうとしているint.Parse(str[0].ToString())のはASCII値を取得することだと思います。

代わりに使用する必要があるのはこれです(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.

于 2012-10-18T17:04:23.703 に答える
1

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;
}
于 2012-10-18T17:05:48.037 に答える
0

I thinks, that number comparison will not work.

First it will return false for string like this "ABC123" even if all letters are capitals. And second there are many national characters that does not fall into range 65..90 even if they are capitals. You should (if you can) use some Char method http://msdn.microsoft.com/en-us/library/d1x97616.aspx

于 2012-10-18T17:12:59.020 に答える
0

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. I like using the extensions in the Extensions.cs NuGet package. It makes this check as simple as:

  1. Add the [https://www.nuget.org/packages/Extensions.cs][1] package to your project.
  2. Add "using Extensions;" to the top of your code.
  3. "smith".IsUpper() will return False whereas "SMITH".IsUpper() will return True. 4. Every other check in the rest of the code is simply MyString.IsUpper().

Your example code would become as simple as:

using Extensions;

//Console.WriteLine(IsCapital("abc"));
Console.WriteLine("abc".IsUpper());
于 2021-01-22T14:46:32.840 に答える
-3
public static bool IsCapital(string str)
{
 return !Regex.IsMatch(str, "[a..z]");
}
于 2012-10-18T17:04:24.400 に答える