[英]How to know a string contains only particular words
How do I check if the string contains only words in an array? 如何检查字符串是否只包含数组中的单词?
I could return true
if the string contains any word in the array using Contains()
如果字符串包含使用
Contains()
的数组中的任何单词,我可以返回true
public bool ValidateString(string strCondition)
{
List<string> words = new List<string>() { "true", "false", "&&", "||", " " };
foreach (string word in words)
{
if(strCondition.Contains(word))
return true;
}
return false;
}
But how can I return false
if the string that is send as parameter( strCondition
) contains any word or alphabet or number or etc etc other than true
, false
, &&
, ||
但是如果作为参数发送的字符串(
strCondition
)包含除true
, false
, &&
, ||
之外的任何单词或字母或数字等等,我怎么能返回false
, ,
?
? Is there any option in
Regex
or can some one come up with a good solution? Regex
有没有选择,或者有人可以提出一个好的解决方案吗?
EDIT 编辑
The following should return true 以下应该返回true
true && false || false && false
true || false && false && false
And the below should return false since it contains a word/number/special characters other than true
, false
, &&
, ||
并且下面应该返回false,因为它包含除了
true
, false
, &&
, ||
之外的单词/数字/特殊字符 , ,
true & false || false < false
true >> false && false && false
true and false 123 false && false
true || false && false xyz false
First, your code suffers from the clbuttic problem : it would return true
for a string that has a word "untrue", because the code does not pay attention to word boundaries. 首先,你的代码会受到克隆问题的困扰 :对于一个单词“ untrue ”的字符串,它会返回
true
,因为代码不会注意字边界。
If you would like to check if no other words are contained, split the string, and check each item against the list of "approved" words: 如果您想检查是否包含其他单词,请拆分字符串,并根据“已批准”单词列表检查每个项目:
var allApproved = strCondition.Split(' ').All(word => words.Contains(word));
This approach implies that words in words
do not contain spaces. 这种方法意味着单词中的
words
不包含空格。
Note that this is not the most efficient approach, although it would work fine for a small list. 请注意,这不是最有效的方法,尽管它适用于小型列表。 If the list is very long, switch to a
HashSet<string>
instead. 如果列表很长,请切换到
HashSet<string>
。
You could do something like this: 你可以这样做:
var regex = new Regex(@"(^(true|false|&&|\|\|)$)");
return regex.IsMatch(input);
Here's another answer which seems to be what you originally asked. 这是另一个答案,似乎是你最初的问题。 If i got it right you wanted to know if only sub-strings are in the text which are in the
List<string>
. 如果我做对了,你想知道文章中是否只有子字符串在
List<string>
。
So "untrue" would return false
because "un" is not in the list of allowed "words" (better sub-strings). 所以“untrue”会返回
false
因为“un”不在允许的“单词”列表中(更好的子字符串)。 But "truetrue" would be allowed. 但是,“真实的”将被允许。
Then have a look at this method which looks more cumbersome but it needs to check something different than the accepted answer: 然后看看这个看起来比较麻烦的方法,但它需要检查一些与接受的答案不同的东西:
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;
}
Note that i've re-ordered the list, the longest strings should come first since that's more efficient in the algorithm above. 请注意,我已经重新排序了列表,最长的字符串应该首先出现,因为在上面的算法中它更有效。
Two examples to demonstrate what it does: 两个例子来说明它的作用:
bool check = ValidateString("truefalse||truetrue"); // true
bool check = ValidateString("truefals||etruetrue"); // false
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.