提取字符串某些部分的最有效方法是什么?

时间:2012-10-12 16:49:37

标签: c#

我有以下对象:

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已切换,我将知道此顺序,我只需要知道如何解析它。

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

string order =“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')];

5 个答案:

答案 0 :(得分: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 */}}
}

答案 1 :(得分: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');

答案 2 :(得分: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;

更新:

通过维护R-G-B部件将其存储为字符串的简单方法是

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;
}

答案 3 :(得分:0)

由于您知道订单,因此可以使用string 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。如果它真的是你解析的颜色,你甚至不需要定义自己的结构。

答案 4 :(得分: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,因为您单独存储它们。如果你需要经常组合它们,你可以在这个类中创建一个函数来组合它们。

相关问题