我想比较一个被修改为默认值的字符串

I want to compare a String that gets modified to its default

我正在编写一种从 .csv 文件中排除元字段的方法。

  public static string metaField(this string test)
    {
        String[] excludeMetafield = { "metafield_1000", "metafield_1001", "metafield_item_1_content_2", "metafield_short_description" };
        String testing = "";

        if (excludeMetafield.Contains(test) || excludeMetafield.Equals(test))
        {
            return test;
        }
        return testing = "Failed";
    }

string test 被传递到方法中时,例如 "short_description" 但是当值被写入 .csv 文件时,它被修改为 "metafield_short_description".

我的 .Contains 没有检测到 "metafield_short_description" CONTAINS "short_description"。我该怎么做?

您目前正在检查数组是否包含您的确切值。您需要实际遍历检查字符串并检查迭代的 String 是否实际包含您的值。

  public static string metaField(this string test)
    {
        String[] excludeMetafield = { "metafield_1000", "metafield_1001", "metafield_item_1_content_2", "metafield_short_description" };
        String testing = "";

        foreach (string s in excludeMetafield) {
            if (s.Contains(test) || s.Equals(test))
            {
                return test;
            }
        }
        return testing = "Failed";
    }

你可以使用 linq

var excludeMetafield =  new string[] { "metafield_1000", "metafield_1001", "metafield_item_1_content_2", "metafield_short_description" };

if (excludeMetafield.Any(e=>e.Contains(test))) return test; 
return "Failed";