C# 如何知道字符串只包含特定的单词

C# 如何知道字符串只包含特定的单词,c#,arrays,regex,string,C#,Arrays,Regex,String,如何检查字符串在数组中是否只包含单词 如果字符串包含数组中的任何单词,我可以使用contains() 下面的应该返回false,因为它包含的单词/数字/特殊字符不是true,false,&&,|, true & false || false < false true >> false && false && false true and false 123 false && false true || false &a

如何检查字符串在数组中是否只包含单词

如果字符串包含数组中的任何单词,我可以使用
contains()

下面的应该返回false,因为它包含的单词/数字/特殊字符不是
true
false
&&
|

true & false || false < false  
true >> false && false && false
true and false 123 false && false
true || false && false xyz false
true&false | false>false&&false&&false
真与假123假与假
真| |假&&false xyz假

首先,您的代码存在以下问题:对于包含单词“untrue”的字符串,它将返回
true
,因为代码不注意单词边界

如果要检查是否包含其他单词,请拆分字符串,并对照“已批准”单词列表检查每个项目:

这种方法意味着
单词中的单词不包含空格

请注意,这并不是最有效的方法,尽管对于一个小列表来说,它可以很好地工作。如果列表很长,请切换到
HashSet


您可以这样做:

var regex = new Regex(@"(^(true|false|&&|\|\|)$)");

return regex.IsMatch(input);

这是另一个答案,似乎是你最初问的。如果我答对了,您想知道
列表中的文本中是否只有子字符串

因此,“untrue”将返回
false
,因为“un”不在允许的“单词”(更好的子字符串)列表中。但“真实”是允许的

然后看看这个方法,它看起来更麻烦,但需要检查与公认答案不同的东西:

List<string> words = new List<string>() { "false", "true", "||", "&&", " " };

public bool ValidateString(string strCondition)
{
    if(string.IsNullOrWhiteSpace(strCondition)) return true;
    int charIndex = 0;
    while (charIndex < strCondition.Length)
    {
        string wordFound = null;
        foreach (string word in words)
        {
            if (word.Length + charIndex > strCondition.Length) 
                continue;
            string substring = strCondition.Substring(charIndex, word.Length);
            if (word == substring)
            {
                wordFound = word;
                break;
            }
        }
        if (wordFound == null)
            return false;
        else if (charIndex + wordFound.Length == strCondition.Length)
            return true;
        else
            charIndex += wordFound.Length;
    }
    return false;
}

因此,您想检查所有
单词是否都在
strCondition
中?因此您不想查找单词而是子字符串?如果
单词中的值都不在
strCondition
中,您的代码将返回
false
,这是您的优先条件?包含
true
false
和&
|
或不包含这些内容?您的算法是正确的,只需先写入优先级条件。如果字符串为“
true%false
,它将返回true,因为“
true
既在字符串中也在数组中。但它应该返回false,因为
%
不在数组中。这有意义吗@RustyI guess OP想知道是否有子字符串不在列表
单词
中。因此,允许使用
| | | |
,但不允许使用
| |-
。是的,蒂姆。确切地我想知道一个不在
单词中的字符串
@timschmelter这是否正是我想要的解决方案。很好。代码行数较少,简单且解释良好。加一。谢谢你,伙计:)不
truetrue
是不允许的
true&&true
true | | true
有效。我感谢你做出的宝贵回答。只要有需要将
truetrue
视为有效字符串的要求,我将确保使用您的代码。感谢Tim和我这边的+1:)
var allApproved = strCondition.Split(' ').All(word => words.Contains(word));
var regex = new Regex(@"(^(true|false|&&|\|\|)$)");

return regex.IsMatch(input);
List<string> words = new List<string>() { "false", "true", "||", "&&", " " };

public bool ValidateString(string strCondition)
{
    if(string.IsNullOrWhiteSpace(strCondition)) return true;
    int charIndex = 0;
    while (charIndex < strCondition.Length)
    {
        string wordFound = null;
        foreach (string word in words)
        {
            if (word.Length + charIndex > strCondition.Length) 
                continue;
            string substring = strCondition.Substring(charIndex, word.Length);
            if (word == substring)
            {
                wordFound = word;
                break;
            }
        }
        if (wordFound == null)
            return false;
        else if (charIndex + wordFound.Length == strCondition.Length)
            return true;
        else
            charIndex += wordFound.Length;
    }
    return false;
}
bool check = ValidateString("truefalse||truetrue"); // true
bool check = ValidateString("truefals||etruetrue"); // false