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

时间:2018-08-03 12:25:47

标签: c# .net string char

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

// 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

4 个答案:

答案 0 :(得分:2)

几个问题:

在您的循环中,您实际上只得到与最后一个字符有关的选项。 “或”子句应予以注意。

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

然后,您需要在结尾处没有一个。如果不包含更多符号,则仅包含符号

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

答案 1 :(得分:1)

检查所有字符是否全部为isSymbol或标点符号。我们在这里返回true。

public static bool ContainsOnlySymbols(this String inputString)
{
    return inputString.ToCharArray().All(x => Char.IsSymbol(x) || Char.IsPunctuation(x));   
}

测试:

string testString = "Starts with"; // => false
string testString = "!="; // => true
string testString = "@@"; // => true
string testString = "!Starts with"; // => false

答案 2 :(得分:1)

我相信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} ");
            }
        }
    }
}

答案 3 :(得分:1)

首先,这个想法很简单:循环string,如果遇到非符号字符,则返回false。直到string的结尾,您都不会遇到非符号字符。 Voilà,返回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仅当您的角色在这些组中时才返回true

  

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));
}
相关问题