commparing有多个枚举字符串的字符串

时间:2017-04-10 06:22:52

标签: c# enums

我有一个像下面这样的枚举

public enum CommonEnum
{
    l, el, ad, an, ar, ash, at, az,Abu
}

然后我需要用字符串检查枚举。喜欢以下

else if (SplitName[1].Contains(Common.CommonEnum.ad.ToString()))
{                   
}

如果我需要检查带有所有枚举值的字符串意味着我想做什么?

我尝试了以下内容

else if (SplitName[1].Contains(Common.CommonEnum.ad.ToString())|| SplitName[1].Contains(Common.CommonEnum.abu.ToString()))
{

}

还有其他技术吗?

2 个答案:

答案 0 :(得分:1)

要检查包含,您可以尝试以下内容:

string SplitName = "sujith";
var resultBool = Enum.GetNames(typeof(CommonEnum)).Any(x => SplitName.Contains(x));
// will give you false 
// gives you true if SplitName = "elsujith"

Working example

答案 1 :(得分:0)

if(System.Enum.IsDefined(typeof(CommonEnum), SplitName[1]))

Fiddle Example