2017-09-23 108 views
-3

我想比較兩個字符串像比較兩個字符串,找到

string1 = "My Name is Something. I do nothing" 
string2 = "My Name is Momething. I do othing" 

的差異,其結果應該是 「我的名字是Momething。我做othing

單詞中的差異應以不同的字體或顏色顯示

我試圖

var difference = string1.Except(string2).ToArray()  

這給diffence

+0

你嘗試過什麼迄今爲止最簡單的方法是什麼? – ne1410s

+0

我試過這個「var difference = string1.Except(string2).ToArray()」這會給出不同的數組 – Ravi

+0

任何人都可以告訴我爲什麼downvoted ?? – Ravi

回答

3

在收集使用Except陣列取第一集合中的所有項目,除了(由類型平等)項中的第二個。由於您的類型是string s它們是char s的集合。你不需要Except,但要檢查哪些字是不同的。

使用Split得到的字符串,其中每個是一個詞的集合,然後用Zip句子比較同一位置的話:

var string1 = "My Name is Something. I do nothing"; 
var string2 = "My Name is Momething. I do othing"; 

var result = string1.Split(' ').Zip(string2.Split(' '), (s1, s2) => new { s1, s2 }) 
        .Where(p => p.s1 != p.s2); 

如果你想確保你做不是隻有空格的部分可以拆分這樣太:

Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries) 

爲了把它放回一個字符串帶標記的差異(例如用<b>圍繞它的HTML大膽,你可以這樣做:

var result = string.Join(" ", string1.Split(' ') 
         .Zip(string2.Split(' '), (s1, s2) => new { s1, s2 }) 
         .Select(pair => pair.s1 != pair.s2 ? $"<b>{pair.s2}</b>" : pair.s1)); 
+0

{System.Linq.Enumerable.WhereEnumerableIterator <<> f__AnonymousType0 >}結果 – Ravi

+0

我得到這個var結果 – Ravi

+0

@Ravi - 這是因爲linq被執行。添加'ToList()'或者在調試時查看'Results View' - 它會執行查詢並顯示結果 –

0

試試這個,這個代碼,如果你的字符串不要比較一字一句,同具有同樣的話數:

string string1 = "My Name is Something. I do nothing tessst"; 
string string2 = "My Name is Momething. I do othing"; 

//Trasnforme string to list with space like separator, and compare string by position 
var List1 = string1.Split(' ').Select((valuestring1, index) => new { valuestring1, index }); 
var List2 = string2.Split(' ').Select((valuestring2, index) => new { valuestring2, index }); 

//Left outer join 
var leftdifference = from word1 in List1 
        from word2 in List2.Where(word2 => word1.index == word2.index).DefaultIfEmpty() 
        where word2 == null || word1.valuestring1 != word2.valuestring2 
        select new { word1, word2 }; 

//Right outer join 
var rightdifference = from word2 in List2 
         from word1 in List1.Where(word1 => word1.index == word2.index).DefaultIfEmpty() 
        where word1 == null || word1.valuestring1 != word2.valuestring2 
         select new { word1, word2 }; 

var fulldifference = leftdifference.Union(rightdifference).ToList(); 

foreach (var item in fulldifference) 
{ 
    string val1 = item.word1 == null ? "NOT PRESENT WORD" : item.word1.valuestring1; 
    string val2 = item.word2 == null ? "NOT PRESENT WORD" : item.word2.valuestring2; 
    int index = item.word1 == null ? item.word2.index : item.word1.index; 

    Console.WriteLine("position {0}, string1 : '{1}' , string2 : '{2}'", index, val1, val2); 
} 
0

我不知道,如果你想在這個網站或別的東西,對於控制檯應用程序:


string[] strArr1 = string1.Split(' ').ToArray(); 
string[] strArr2 = string2.Split(' ').ToArray(); 

for (int i = 0; i < strArr1.Length; i++) 
{ 
    if(string.Compare(strArr1[i], strArr2[i]) != 0) 
    { 
     Console.BackgroundColor = ConsoleColor.Blue; 
     Console.ForegroundColor = ConsoleColor.White; 
     Console.Write(strArr2[i]); 
     Console.ResetColor(); 
     Console.Write(" "); 
    } 
    else 
    { 
     Console.Write(strArr2[i] + " "); 
     Console.Write(" "); 
    } 
} 
0

大概做

 String[] str1 = "foo foo ffo foo".Split(' '); 
     String[] str2 = "foo foo foo foo".Split(' '); 
     //if you want to push differences to a list just uncomment TODO lines 
     //List<String> result = new List<String>(); TODO 

     //int index = 0; TODO 
     for(int i = 0; i < str1.Length; i++) 
     { 
      if (str1[i] != str2[i]) 
      { 
       //str2[i] here is a different word 
       //result.Add(str2[i]); TODO 
       //index++; TODO 
      } 
     }