2017-07-14 53 views
-6

一個leastCommon字符串我有一個字符串的列表:查找列表

{"\\\\eZREUApp01.EU.abc.com\\eZR_Data\\ALSTOM GIS\\Review_Files", 
"\\\\eZREUApp01.EU.abc.com\\eZR_Data\\ALSTOM GIS\\ADP_Processes", 
"\\\\EZRSEARCH01.eu.abc.com\\road\\EZR_ALSTOM GIS_7\\", 
"\\\\eZREUApp01.EU.abc.com\\eZR_Data\\ALSTOM GIS\\Production_Files\\009", 
"\\\\EZRSEARCH01.EU.abc.com\\table\\EZR_Alstom_GIS_7\\", 
"\\\\eZREUApp01.EU.abc.com\\eZR_Data\\ALSTOM GIS\\Production_Files"} 

現在我想在C#中輸出:

{"\\\\eZREUApp01.EU.abc.com\\eZR_Data\\ALSTOM GIS" 
"\\\\EZRSEARCH01.eu.abc.com\\road\\EZR_ALSTOM GIS_7" 
"\\\\EZRSEARCH01.EU.abc.com\\table\\EZR_Alstom_GIS_7 "} 

任何人都可以幫我嗎?

+6

什麼是「leastCommon」字符串?我對你想要做什麼有一個模糊的想法,但是你沒有提供足夠的信息來將它們變成真正的「規則」或代碼。 –

+5

你有什麼試過呢? –

+1

'「\\\\ eZREUApp01.EU.abc.com \\ eZR_Data \\ ALSTOM GIS」「甚至不會出現在您輸入的字符串列表中。它如何產生輸出? – RBT

回答

2

這裏是你如何做到這一點的例子。 您必須執行以下步驟:
1.拆分字符串並遍歷結果數組以獲取最小數量的子進程。
2.轉到拆分數組,並在第一步中查找元素並將它們連接回去。

我正在使用HashSet,以便結果Set將不同。

using System; 
using System.Collections.Generic; 
using System.Linq; 

namespace Rextester 
{ 
    public class Program 
    { 
     public static void Main(string[] args) 
     { 
      string[] arr ={"\\\\eZREUApp01.EU.abc.com\\eZR_Data\\ALSTOM GIS\\Review_Files", 
          "\\\\eZREUApp01.EU.abc.com\\eZR_Data\\ALSTOM GIS\\ADP_Processes", 
          "\\\\EZRSEARCH01.eu.abc.com\\road\\EZR_ALSTOM GIS_7\\", 
          "\\\\eZREUApp01.EU.abc.com\\eZR_Data\\ALSTOM GIS\\Production_Files\\009", 
          "\\\\EZRSEARCH01.EU.abc.com\\table\\EZR_Alstom_GIS_7\\", 
          "\\\\eZREUApp01.EU.abc.com\\eZR_Data\\ALSTOM GIS\\Production_Files"}; 

      string[][] newArr = new string[arr.Length][]; 

      for (int i = 0; i < newArr.GetLength(0); i++) 
      { 
       newArr[i] = arr[i].Split(new char[] { '\\' },StringSplitOptions.RemoveEmptyEntries); 
      } 

      var min = newArr.Min(x => x.Length); 

      HashSet<string> resultSet = new HashSet<string>(); 
      foreach(var a in newArr) 
      { 
       resultSet.Add("\\\\" + a.Take(min).Aggregate((x,y)=>x+"\\"+y)); 
      } 
      foreach(var a in resultSet) 
      { 
       Console.WriteLine(a); 
      } 
     } 
    } 
} 
+0

謝謝你的代碼太棒了!但在添加到結果集中時,您已將3作爲靜態值。我們不確定這些意見,所以我們不能拿這樣的價值。 – Divya

+0

@Divya啊忘了把它改回來,它必須是'min'而不是'3'。我已經更新了答案 –

+0

非常感謝你! – Divya