0

Is there a clever way to get an enum value by comparing a substring a la String.Contains with the enum? I checked this implementation (Convert a string to an enum in C#), but would like to have an intelligent extension doing the same but including substrings comparison, something like TryParse with an option to check the whole substring of the enum values.

    public static void Main()
{
    string a = "HANS";
    GetName(a); // prints Hans
    
    string a1 = "Peter";
    GetName(a1); // should print enum value "WolfgangPeterDietrich " containing the substring "Peter"
}

public enum MyNames
{
    Hans = 0,
    WolfgangPeterDietrich = 3,
}

public static void GetName(string a)
{
    MyNames dif;
    // this works fine, ignore case
    if (Enum.TryParse(a, true, out dif))
    {      
        System.Console.WriteLine(dif.ToString());
    }
    // ToDo: check for substring ??
}
4
  • 2
    What if more than one enum value contains the substring? Commented Oct 6, 2021 at 11:49
  • 3
    Enum.GetNames<Names>().Where(n => c.Contains(a, StringComparison.OrdinalIgnoreCase)) might be a start? Commented Oct 6, 2021 at 11:50
  • Also, if it don't find, what it should print? Is it case sensitive or insensitive? Commented Oct 6, 2021 at 11:52
  • First occurence should be fine, and if it dont find, just do nothing is fine Commented Oct 6, 2021 at 11:59

3 Answers 3

1

This approach takes the first occurrence found, and is case insensitive.

public static void GetName(string a)
{
    string? result = Enum.GetNames<MyNames>()
        .FirstOrDefault(x => x.Contains(a, StringComparison.OrdinalIgnoreCase));
    System.Console.WriteLine(result);
}
Sign up to request clarification or add additional context in comments.

2 Comments

that does exactly the job, thanks a lot for your help!
just changed to Enum.GetNames(typeof(MyNames)).FirstOrDefault
0

Just a prototype to fix with boundary cases and assertions:

public static T EnumNameContains<T>(string substringOfName) where T: struct
{
    return (T)Enum.Parse(typeof(T), 
        Enum.GetNames(typeof(T)).FirstOrDefault(
            name => name.IndexOf(substringOfName,StringComparison.CurrentCultureIgnoreCase) >= 0));
}

I guess can help.

1 Comment

0

You can try this

public static void GetName(string a)
        {
            MyNames dif;
            // this works fine, ignore case
            if (Enum.TryParse(a, true, out dif))
            {
                System.Console.WriteLine(dif.ToString());
            }
            else
            {
                var result = Enum.GetNames(typeof(MyNames)).Where(dd => dd.Contains(a, StringComparison.OrdinalIgnoreCase)).FirstOrDefault();
                System.Console.WriteLine(result);
            }
            
        }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.