string[].Contains 找不到匹配项,即使文本在数组中也是如此

string[].Contains does not find a match even if the text is in the array

我已经在我的程序中显示了,可以清楚地看到要搜索的字符串存在。

是否有 'invisible' 个字符阻止它工作?

    var items = new List<string>(currComponent.Split(new string[] 
    { "\r\n", "\n" }, StringSplitOptions.None));
    string feedback = string.Empty;
                foreach (string i in items)
                {
                    feedback += i + ". ";
                }
                ScriptingObject.WriteLogMessage
                 ("feedback:" + feedback + "<EOF>", true);
   if (items.Contains(attributeDescription))

显示的结果显示变量feedback包含短语'Flue', 但是 items.Contains("Flue") 没有找到。

Message: Status message: 2015-01-09 10:36:18 | feedback:_ ,. Boiler Flue Type. 
Boiler make. Boiler Model. Gas Appliance Safety Protector. __.. . <EOF>

Next message: unable to process an Update Location record ...because of error:
item Flue not found .

因为 items 中的字符串不是 "Flue",而是 "Boiler Flue Type"List.Contains() 搜索完全匹配,请记住您调用的是 List.Contains() 而不是 String.Contains().

请注意,连接字符串的方法更简单:

string feedback = String.Join(". ", items);

对于 LINQ,您可以使用这个:

if (items.Any(x => x.Contains(attributeDescription))) { }

如果您没有明确需要 List<string>,您也可以保留 String.Split() return 类型 (string[]),将所有内容放在一起:

var items = currComponent.Split(new string[] { "\r\n", "\n" },
    StringSplitOptions.None);

ScriptingObject.WriteLogMessage("feedback: " 
    + String.Join(". ", items)
    + "<EOF>", true);

if (items.Any(x => x.Contains(attributeDescription))) {
}