[英]Compare string array to enum C#(UNITY)
How can I compare the string data i use to my enum 如何将我使用的字符串数据与枚举进行比较
I made this enum 我做了这个枚举
public enum BeadColor
{
BLUE,
RED,
GREEN,
NONE,
}
public BeadColor previousColor = BeadColor.NONE;
and I will create a string array like this 我将创建一个这样的字符串数组
string str = {"P", "B", "T"};
for doing this to an enum 对一个枚举这样做
public enum BeadColor
{
BLUE = "P",
RED = "B",
GREEN = "T",
NONE,
}
So if this is possible i would like to use the enum
to check if my string BigRoadData.Contains(/*the enum BLUE for example*/)
因此,如果可能的话,我想使用
enum
检查我的字符串BigRoadData.Contains(/*the enum BLUE for example*/)
cause instead of doing the normal way 原因而不是正常方式
if(BigRoadData.Contains(str[0])){
//print a
}
I would like to do it "IF POSSIBLE" like this 我想像这样“如果可能”
if(BigRoadData.Contains(/*the enum blue for example*/)){
//print a
}
CustomClass.cs CustomClass.cs
public string BigRoadData = "";
public string[] strData = {"P ,B ,B ,P ,B ,B ,B ,B ,B ,B ,P ,P ,B ,P "};
public void MakeBigRoad(string data){
BigRoadData = data;
for(int i = 0; i < strData.Length; i++){
BigRoadData += strData [i];
BigRoadData += ",";
}
}
public void ScoreBoardCondition(){
if(BigRoadData.Contains(....)){
}
}
MainClass 主类
Here i will call it like this 在这里我会这样称呼它
CustomClass.ScoreBoardCondition();
On my CustomClass.cs 在我的CustomClass.cs上
public enum _Data
{
P,
B,
T,
}
public enum _BeadColors
{
BLUE,
RED,
NONE,
}
public _Data previousColor = _BeadColors.NONE;
class CustomClass{
}
//extension method for contains
public static bool Contains(this string input, _Data data){
string[] str = { "P", "B", "T" };
if(data == BaccaratScoreboard._Data.P){
return input.Contains (str [0]);
}
if(data == BaccaratScoreboard._Data.B){
return input.Contains (str [1]);
}
if(data == BaccaratScoreboard._Data.T){
return input.Contains (str [2]);
}
There are many ways to do this. 有很多方法可以做到这一点。
1 .You can map your Enum
to the data in the string array with a Dictionary
then use Dictionary.ContainsKey(YourEnum.Value)
. 1。您可以映射您
Enum
为与字符串数组中的数据Dictionary
然后使用Dictionary.ContainsKey(YourEnum.Value)
2 .You make the names of the enum variable the-same with the name of the values in the string array then use Enum.Parse
to make this conversion and compare them. 2。使enum变量的名称与字符串数组中值的名称相同,然后使用
Enum.Parse
进行此转换并进行比较。
3 .You can make an Contains
extension method for the string
class that hides your BigRoadData.Contains(str[0])
. 3。您可以为隐藏
BigRoadData.Contains(str[0])
的string
类创建一个Contains
扩展方法。 This function will take BeadColor
enum as argument and allows you to compare with that instead of str[0]
. 该函数将
BeadColor
枚举作为参数,并允许您与之比较而不是str[0]
。
I will provide an example for the last method. 我将提供最后一个方法的示例。 You can implement the other ones yourself if you wish to use them instead.
如果您想替代使用其他代码,则可以自己实现。
The enum: 枚举:
public enum BeadColor
{
BLUE, //P
RED, //B
GREEN, //T
NONE,
}
The extension method: 扩展方法:
public static class ExensionMethod
{
public static bool Contains(this string input, BeadColor cColor)
{
string[] str = { "P", "B", "T" };
if (cColor == BeadColor.BLUE)
return input.Contains(str[0]);
if (cColor == BeadColor.RED)
return input.Contains(str[1]);
if (cColor == BeadColor.GREEN)
return input.Contains(str[2]);
return false;
}
}
The extension method maps "P" to BeadColor.BLUE
, "B" to BeadColor.RED
and "T" to BeadColor.GREEN
and makes it possible to check if the color exists using the BeadColor
enum. 扩展方法“P”映射到
BeadColor.BLUE
“B”,以BeadColor.RED
和“T”,以BeadColor.GREEN
和使得能够检查是否存在颜色使用BeadColor
枚举。
Usage : 用法 :
It works on string 它适用于字符串
public string BigRoadData = "";
void Start()
{
if (BigRoadData.Contains(BeadColor.BLUE))
{
}
}
You can't use string
as the type of your enum
. 您不能使用
string
作为enum
的类型。
You can try char
, but is not recommended. 您可以尝试使用
char
,但不建议这样做。
See this question and answers: https://stackoverflow.com/q/8588384/2025364 看到这个问题和答案: https : //stackoverflow.com/q/8588384/2025364
By the way, concatenating strings like that is VERY bad: BigRoadData += strData [i];
顺便说一句,串联这样的字符串非常不好:
BigRoadData += strData [i];
Use String.Concat
(or StringBuilder
) instead. 请改用
String.Concat
(或StringBuilder
)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.