2017-04-10 140 views
0

commparing字符串我有一个像下面多个枚举字符串

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())) 
{ 

} 

是否有任何其他技术?

+1

会是什么'SplitName [1]'的价值,任何样品? –

回答

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

Fiddle Example

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