2017-06-05 50 views
2

我有一个数字代码,可以检测用户穿着什么衣服。以下是一些随机数字代码的例子。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-92. 

的双字母代码指示衣服的类型,例如:

  • lg =腿
  • ch =胸部
  • sh =鞋
  • he =头
  • 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); 
} 
+0

我有一个很难理解的问题。对于统一的组成部分,你有一个统一的字符串。你希望能够将这个统一的字符串转换成它的鞋子或胸部等效物? – Neil

+0

是的,我想要替换数字字符串中统一字符串中的所有身体部位,因此数字字符串是穿着制服的用户。 –

回答

2

我建议使用的LINQ正则表达式(解析各行):

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

结果(提取itemsToGet名,为了保存):

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. 
+0

一个很好的解决方案,但这是一个非常长的'Linq'链,可能需要一些解释给经验较少的用户。 (也可以使用免责声明,这需要C#6.0。) – Pharap

+1

@Pharap:很对,谢谢!我编辑了答案(添加了注释,字符串插值 - C#6.0功能 - 提供了替代代码) –