2017-06-05 49 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功能 - 提供了替代代碼) –