0

我有以下对象:

public class Color
{
   public string RGB = "000225123";

}

我需要能够解析出 R、G 和 B 组件,但它们可能不是那么精确,但是,我会知道顺序。所以一种方式,它可能是RGB,但另一种方式它可能是RBG

我想过这样的事情:

string G =  RGB.Substring(RGB.IndexOf('2'),3);

但是在这里我碰巧知道 G 的开始和结束位置,所以我可以对其进行硬编码,这让我想到了另一个问题,即存储订单的最佳方式是什么。我曾想过做类似的事情:

string order = "RGB";

string RGB = "000225123";

string R = RGB.Substring(order.IndexOf('R'),3);

上述方法可行,但是对于每个部分可以是不同长度的情况,R例如可以是 2 或 3 个字符,那是我单独存储的东西还是我将其存储在订单中?

我可能有这样的数字:

28-34-29-000-00.0

或者它可能是有序的

29-34-28-000-00.0

在上面的2829被切换,我会知道这个顺序,我只需要知道如何解析它。

这是一个更现实的场景和解决方案,但我不确定它是否足够有效:

字符串顺序=“TSR”;

string value = "20-10-28-0000-0011";

string[] tokens = value .Split('-');

string t= tokens[order.IndexOf('T')];

string s= tokens[order.IndexOf('S')];

string r= tokens[order.IndexOf('R')];
4

5 回答 5

2

我将创建一个包含解析方法的接口。

例如,

IParseColorString
{
  ColorParts Parse(String s);
}

ColorParts
{
  public string R {get;}
  public string G {get;}
  public String B {get;}
  // or if you wanted the int directly have int return type instead of string
}

然后让所有具有适当顺序的类派生接口:

ParseRGB : IParseColorString
{
    public ColorParts Parse(String s)
    {
       //  parsing logic for RGB
    }
}

ParseRBG : IParseColorString
{
  public ColorParts Parse(String s)
  {
     // parsing logic for RBG
  }
}

然后随心所欲地使用它们。您甚至可以拥有一个将它们作为静态实例的工厂

ColorParsingFactory
{
    public static IParseColorString ParseRGB {get{/* gets the RGB parser */}}
    public static IParseColorString ParseRBG {get{/* gets the RBG parser */}}
}
于 2012-10-12T16:56:21.193 回答
1

您可以使用这样的函数从字符串中提取组件:

public static int GetComponent(string data, string format, char component) {
  return Int32.Parse(new String(data.Where((c, i) => format[i] == component).ToArray()));
}

用法:

string color = "000225123";
string format = "RRRGGGBBB";

int red = GetComponent(color, format, 'R');
int green = GetComponent(color, format, 'G');
int blue = GetComponent(color, format, 'B');

它适用于您可以这样描述的任何格式:

string time = "2012-10-12 19:02";
string format = "YYYY MM DD hh mm";

int year = GetComponent(time, format, 'Y');
int month = GetComponent(time, format, 'M');
int day = GetComponent(time, format, 'D');
int hour = GetComponent(time, format, 'h');
int minute = GetComponent(time, format, 'm');
于 2012-10-12T16:59:08.227 回答
0

如果我理解正确,还有其他一些服务可以给你一个订单和一个 RGB 字符串?

在这种情况下,让我们尝试忠实地存储类型在到达时转换数据

public class Color{

public int R{
    get;
    set; 
    }


public int G{
    get;
    set; 
    }

public int B{
    get;
    set; 
    }

public Color(string mixedRGB, string order){
    R = Int32.Parse(mixedRGB.Substring(order.IndexOf('R'),3));
    G = Int32.Parse(mixedRGB.Substring(order.IndexOf('G'),3));
    B= Int32.Parse(mixedRGB.Substring(order.IndexOf('B'),3));
}

如果您有内存限制,这将为您节省空间,并避免为您的对象分配无意义的值。(RGB字符串“DF)_3nAv1”是什么颜色?)

您将知道 R、G 和 B 对应的值是什么,因为您将它们分开存储。如果你需要经常组合它们,你可以在这个类中创建一个函数来组合它们。

于 2012-10-12T17:10:40.643 回答
0

将颜色转换为数字的更好方法是使用 ARGB 格式

Color c = Color.Aquamarine;
// Or if you have R-G-B values:  Color c = Color.FromArgb(111,222,333);
int argb = c.ToArgb(); // --> -8388652
string s = argb.ToString(); // --> "-8388652"

// Backwards
argb = Int32.Parse(s);
c = Color.FromArgb(argb);

int R = c.R;
int G = c.G;
int B = c.B;

更新:

通过维护 RGB 部分将其存储为字符串的简单方法是

string s1 = "RGB;123;255;77";
string s2 = "RBG;123;77;255";

string[] parts = s1.Split(';');

int r, g, b;
switch (parts[0]) {
    case "RGB":
        r = Int32.Parse(parts[1]);
        g = Int32.Parse(parts[2]);
        b = Int32.Parse(parts[3]);
        break;
    case "RBG":
        r = Int32.Parse(parts[1]);
        b = Int32.Parse(parts[2]);
        g = Int32.Parse(parts[3]);
        break;
    default:
        r = 0;
        b = 0;
        g = 0;
        break;
}
于 2012-10-12T16:59:47.070 回答
0

由于您知道顺序,因此可以使用字符串 indexer获取值。

从 MSDN 公然窃取的示例:

string str1 = "Test";
for (int ctr = 0; ctr <= str1.Length - 1; ctr++ )
   Console.Write("{0} ", str1[ctr]);

// The example displays the following output: 
//      T e s t         

您可能还想查看System.Drawing.Color. 如果它真的是你正在解析的颜色,你甚至不需要定义你自己的结构。

于 2012-10-12T17:04:23.513 回答