Search code examples
c#if-statementwildcard

Wildcards in "IF" Statement


I am trying to create a filter using a wildcard. I have a string List of all Layers but I want to excluded certain ones "0", "Defpoints" and also any layers that end with -LT, -LT1, and so forth.

if ((ltr.Name != "0") && (ltr.Name.ToUpper() != "DEFPOINTS") && (ltr.Name != "*LT*"))
{
    AllLayerList.Add(ltr.Name);
}

Hope someone can help, thanks.

I just want these LT layers excluded from my list. Could not find anything online that would help.


Solution

  • As @codeulike mentioned in their comment, C# does not do those types of string comparisons in that way. With your example, you can use string.Contains:

    if (ltr.Name != "0" && ltr.Name.ToUpper() != "DEFPOINTS" && !ltr.Name.Contains("LT"))
    {
        AllLayerList.Add(ltr.Name);
    }
    

    If other cases may have "LT" in the name and should be included, then it may need to be changed to be more specific about the cases to exclude.

    If "LT" can be lowercase, you can use !ltr.Name.Contains("LT", StringComparison.OrdinalIgnoreCase) instead.