检查字符是否为符号的意外结果

我正在创建一个字符串扩展来检查一个字符串是否都是符号,但是它没有像我期望的那样工作,到目前为止我有以下内容:


// Class for: String extensions

public static class StringExtension

{

    // Method for: Determining if a string contains only symbols

    public static bool ContainsOnlySymbols(this String inputString)

    {

        // Identifiers used are:

        bool containsMore = false;


        // Go through the characters of the input string checking for symbols

        foreach (char character in inputString.ToCharArray())

        {

            // This line needs || Char.IsPunctuation(character) also 

            // Credit: @asantaballa

            containsMore = Char.IsSymbol(character) ? false : true;

            if (containsMore)

            {

                return containsMore;

            }

        }


        // Return the results

        return containsMore; // Edited after answer: <-- mistake here

    }

}

现在,如果我在以下两个字符串上使用此扩展名,则会得到与我期望看到的相反的结果:


string testString = "!=";

我希望这都是符号,但是


I expect: testString.ContainsOnlySymbols() => true

I get:    testString.ContainsOnlySymbols() => false

现在,如果我使用下一个测试字符串:


string testString = "Starts with";

我希望这没有符号


I expect: testString.ContainsOnlySymbols() => false

I get:    testString.ContainsOnlySymbols() => true


catspeake
浏览 180回答 3
3回答

慕娘9325324

几个问题:在您的循环中,您实际上只获得与最后一个字符相关的选项。和或条款应该照顾它。containsMore = containsMore || !(Char.IsSymbol(character) || Char.IsPunctuation(character));然后,你最后需要一个 not。如果它不包含更多,那么它唯一的符号return ! containsMore;您可能还需要一个特殊情况来说明如何处理空字符串。不知道你想如何处理。如果空字符串应该返回 true 或 false,那将是您的选择。您可以使用单线完成此操作。请参阅这些示例。&nbsp; &nbsp; &nbsp; &nbsp; string x = "@#=";&nbsp; &nbsp; &nbsp; &nbsp; string z = "1234";&nbsp; &nbsp; &nbsp; &nbsp; string w = "1234@";&nbsp; &nbsp; &nbsp; &nbsp; bool b = Array.TrueForAll(x.ToCharArray(), y => (Char.IsSymbol(y) || Char.IsPunctuation(y))); // true&nbsp; &nbsp; &nbsp; &nbsp; bool c = Array.TrueForAll(z.ToCharArray(), y => (Char.IsSymbol(y) || Char.IsPunctuation(y))); // false&nbsp; &nbsp; &nbsp; &nbsp; bool e = Array.TrueForAll(w.ToCharArray(), y => (Char.IsSymbol(y) || Char.IsPunctuation(y))); // false

MMMHUHU

我相信 IsSymbol 方法会检查一组非常具体的字符。你可能想做:containsMore = (Char.IsSymbol(character) || Char.IsPunctuation(character)) ? false : true;编写了一个快速程序来显示字符的结果并且确实显示了症状。甚至可能是您的应用程序所需要的只是 IsPunctuation。33/!: IsSymbol=False, IsPunctuation=True程序using System;namespace csharptestchis{&nbsp; &nbsp; class Program&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; static void Main(string[] args)&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; for (int i = 0; i <= 255; i++)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; char ch = (char)i;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; bool isSymbol = Char.IsSymbol(ch);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; bool isPunctuation = Char.IsPunctuation(ch);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Console.WriteLine($"{i}/{ch}: IsSymbol={isSymbol}, IsPunctuation={isPunctuation} ");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }}

墨色风雨

首先,这个想法很简单:你循环你的string,如果你遇到一个非符号的字符,返回false。直到结束,string你才不会遇到一个字符非符号。瞧,回来true。public static bool ContainsOnlySymbols(string inputString){&nbsp; &nbsp; // Identifiers used are:&nbsp; &nbsp; bool containsMore = false;&nbsp; &nbsp; // Go through the characters of the input string checking for symbols&nbsp; &nbsp; foreach (char character in inputString)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; containsMore = Char.IsSymbol(character) ? false : true;&nbsp; &nbsp; &nbsp; &nbsp; if(!containsMore)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return false;&nbsp; &nbsp; }&nbsp; &nbsp; // Return the results&nbsp; &nbsp; return true;}其次,你的代码有问题,IsSymbol只有当你的角色在这些组中时才返回真MathSymbol、CurrencySymbol、ModifierSymbol 和 OtherSymbol。幸运的是,!不要在这些组中。这意味着 "!=" 返回false。因此,您必须包括其他条件,例如:public static bool ContainsOnlySymbols(string inputString){&nbsp; &nbsp; // Go through the characters of the input string checking for symbols&nbsp; &nbsp; return inputString.All(c => Char.IsSymbol(c) || Char.IsPunctuation(c));}或者您必须编写自己的方法来确定哪些符号是可接受的,哪些是不可接受的。或者,如果字符串不包含数字和字母,则可以将其视为符号。你可以做public static bool ContainsOnlySymbols(string inputString){&nbsp; &nbsp; // Go through the characters of the input string checking for symbols&nbsp; &nbsp; return !inputString.Any(c => Char.IsLetterOrDigit(c));}
打开App,查看更多内容
随时随地看视频慕课网APP