Home > Software engineering >  I want to compare a String that gets modified to its default
I want to compare a String that gets modified to its default

Time:01-29

I am writing a method to exclude metafields from a .csv file.

  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";
    }

When the string test gets passed into the method it is for example "short_description" but when the value is written to the .csv file it gets modified to "metafield_short_description".

My .Contains is not detecting that "metafield_short_description" CONTAINS "short_description". How can I do this?

CodePudding user response:

Your currently checking if the array contains your exact value. You need to actually loop over your check strings and check if the iterated String physically contains your value.

  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";
    }

CodePudding user response:

you can use 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";
  •  Tags:  
  • Related