C#:将制服与图形代码合并?

C#:将制服与图形代码合并?,c#,C#,我有一个数字代码,可以检测用户穿的衣服。下面是一些随机数字代码的示例 lg-285-76.hr-155-31.sh-300-92.ca-1819-63.hd-209-2.ch-3030-76. he-3149-1331.sh-3035-110.hr-170-61.fa-3276-72.ch-255-110.hd-209-2.lg-280-1331. ch-210-110.hd-209-7.hr-828-1407.he-3082-63.lg-280-1408.cp-3309-77.sh-290-9

我有一个数字代码,可以检测用户穿的衣服。下面是一些随机数字代码的示例

lg-285-76.hr-155-31.sh-300-92.ca-1819-63.hd-209-2.ch-3030-76.
he-3149-1331.sh-3035-110.hr-170-61.fa-3276-72.ch-255-110.hd-209-2.lg-280-1331.
ch-210-110.hd-209-7.hr-828-1407.he-3082-63.lg-280-1408.cp-3309-77.sh-290-92.
两个字母代码表示服装类型,例如:

  • lg
    =腿
  • ch
    =胸部
  • sh
    =鞋
  • =头
  • hr
    =头发
我想做的是将图形代码与统一代码合并。假设我有一个统一的代码,我希望数字代码与之合并,这套制服包含一件T恤和一条裤子。它将包含腿部的
lg
标签和胸部的
ch
标签

统一字符串:
lg-285-76.sh-300-92.ch-3030-76.

我想做的是替换主图形代码中的腿部、胸部等,并替换为统一字符串中的腿部、胸部等,以下是我迄今为止尝试的:

public static string ReturnUniform(string uniform, string figure)
{
    string[] uniformParts = uniform.Split('.');
    string[] figureParts = uniform.Split('.');

    foreach (string uniformPart in uniformParts)
    {
        string[] childUniformParts = uniformPart.Split('-');

        if (figure.Contains("." + childUniformParts[0]))
        {
            // remove it from figure parts and replace it with the uniform one here
        }
    }

    return string.Join("", figureParts);
}
我建议使用Linq和正则表达式(解析每一行):

结果(提取的
itemsToGet
名称,保留顺序):


我很难理解这个问题。你有一个统一的字符串来表示一个统一的组件,比如说腿。你想将制服串转换成鞋或胸部的等效物吗?是的,我想替换人形串中制服串中的任何身体部位,因此人形串就是穿着制服的用户。这是一个很好的解决方案,但这是一条非常长的
Linq
链,可能需要对经验不足的用户进行一些解释。(也可以使用免责声明,这需要C#6.0。)@Pharap:完全正确,谢谢!我已经编辑了答案(添加了注释,并提供了字符串插值的-C#6.0特性-可选代码)
  string[] figures = new string[] {
    "lg-285-76.hr-155-31.sh-300-92.ca-1819-63.hd-209-2.ch-3030-76.",
    "he-3149-1331.sh-3035-110.hr-170-61.fa-3276-72.ch-255-110.hd-209-2.lg-280-1331.",
    "ch-210-110.hd-209-7.hr-828-1407.he-3082-63.lg-280-1408.cp-3309-77.sh-290-92.",
  };

  // we want "lg", "sh", "ch" in this particular order
  string[] itemsToGet = new string[] { "lg", "sh", "ch" };

  var result = figures
    .Select(figure => Regex
       // Parse each line into name  (e.g. "sh")
       //                      value (e.g. "3035-110")
       //                      index in the itemsToGet (-1 if absent)
       // with a help of regular expression
      .Matches(figure, @"(?<name>[a-z]+)-(?<value>[0-9]+-[0-9]+)\.")
      .OfType<Match>()
      .Select(match => new {
         name = match.Groups["name"].Value,
         value = match.Groups["value"].Value,
         index = Array.IndexOf(itemsToGet, match.Groups["name"].Value) })
       // we want items' names that are presented in itemsToGet only...
      .Where(item => item.index >= 0) 
       // ... and in the right order (as they mentioned in itemsToGet) 
      .OrderBy(item => item.index)
       // item format: "name.value."
       // $"..." (string interpolation) is a C# 6.0 syntax; C# 5.0- alternative  
       // .Select(item => string.Format("{0}.{1}.", item.name, item.value))
      .Select(item => $"{item.name}.{item.value}."))
     // concat all the items back into a string e.g. "lg.285-76.sh.300-92.ch.3030-76."
    .Select(items => string.Concat(items))
     // finally, let's materialize records into an array 
    .ToArray(); 
   Console.Write(string.Join(Environment.NewLine, result));
lg.285-76.sh.300-92.ch.3030-76.
lg.280-1331.sh.3035-110.ch.255-110.
lg.280-1408.sh.290-92.ch.210-110.