为了账号安全,请及时绑定邮箱和手机立即绑定

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

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

C#
catspeake 2021-11-14 14:59:39
我正在创建一个字符串扩展来检查一个字符串是否都是符号,但是它没有像我期望的那样工作,到目前为止我有以下内容:// Class for: String extensionspublic 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() => trueI get:    testString.ContainsOnlySymbols() => false现在,如果我使用下一个测试字符串:string testString = "Starts with";我希望这没有符号I expect: testString.ContainsOnlySymbols() => falseI get:    testString.ContainsOnlySymbols() => true
查看完整描述

3 回答

?
慕娘9325324

TA贡献1783条经验 获得超4个赞

几个问题:


在您的循环中,您实际上只获得与最后一个字符相关的选项。和或条款应该照顾它。


containsMore = containsMore || !(Char.IsSymbol(character) || Char.IsPunctuation(character));

然后,你最后需要一个 not。如果它不包含更多,那么它唯一的符号


return ! containsMore;

您可能还需要一个特殊情况来说明如何处理空字符串。不知道你想如何处理。如果空字符串应该返回 true 或 false,那将是您的选择。


您可以使用单线完成此操作。请参阅这些示例。


        string x = "@#=";

        string z = "1234";

        string w = "1234@";


        bool b = Array.TrueForAll(x.ToCharArray(), y => (Char.IsSymbol(y) || Char.IsPunctuation(y))); // true

        bool c = Array.TrueForAll(z.ToCharArray(), y => (Char.IsSymbol(y) || Char.IsPunctuation(y))); // false

        bool e = Array.TrueForAll(w.ToCharArray(), y => (Char.IsSymbol(y) || Char.IsPunctuation(y))); // false


查看完整回答
反对 回复 2021-11-14
?
MMMHUHU

TA贡献1834条经验 获得超8个赞

我相信 IsSymbol 方法会检查一组非常具体的字符。你可能想做:


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

编写了一个快速程序来显示字符的结果并且确实显示了症状。甚至可能是您的应用程序所需要的只是 IsPunctuation。


33/!: IsSymbol=False, IsPunctuation=True

程序


using System;


namespace csharptestchis

{

    class Program

    {

        static void Main(string[] args)

        {

            for (int i = 0; i <= 255; i++)

            {

                char ch = (char)i;

                bool isSymbol = Char.IsSymbol(ch);

                bool isPunctuation = Char.IsPunctuation(ch);

                Console.WriteLine($"{i}/{ch}: IsSymbol={isSymbol}, IsPunctuation={isPunctuation} ");

            }

        }

    }

}


查看完整回答
反对 回复 2021-11-14
?
墨色风雨

TA贡献1853条经验 获得超6个赞

首先,这个想法很简单:你循环你的string,如果你遇到一个非符号的字符,返回false。直到结束,string你才不会遇到一个字符非符号。瞧,回来true。


public static bool ContainsOnlySymbols(string inputString)

{

    // Identifiers used are:

    bool containsMore = false;


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

    foreach (char character in inputString)

    {

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

        if(!containsMore)

            return false;

    }


    // Return the results

    return true;

}

其次,你的代码有问题,IsSymbol只有当你的角色在这些组中时才返回真


MathSymbol、CurrencySymbol、ModifierSymbol 和 OtherSymbol。


幸运的是,!不要在这些组中。这意味着 "!=" 返回false。


因此,您必须包括其他条件,例如:


public static bool ContainsOnlySymbols(string inputString)

{

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

    return inputString.All(c => Char.IsSymbol(c) || Char.IsPunctuation(c));

}

或者您必须编写自己的方法来确定哪些符号是可接受的,哪些是不可接受的。


或者,如果字符串不包含数字和字母,则可以将其视为符号。你可以做


public static bool ContainsOnlySymbols(string inputString)

{

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

    return !inputString.Any(c => Char.IsLetterOrDigit(c));

}


查看完整回答
反对 回复 2021-11-14
  • 3 回答
  • 0 关注
  • 150 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信