2017-07-06 101 views
0

我必須比較其中包含HTML標記的兩個字符串。我想忽略HTML標籤本身的外殼,但是要比較HTML標籤之外的其他字符串的外殼。希望在編寫這個邏輯時有所幫助。 例如,我想忽略下面字符串中TD標籤的外殼。C#字符串比較忽略區分大小寫HTML標記

String1: "<td>this is case-sensitive</td>" 

String2: "<TD>THIS is CASE-sensitive</TD>" 
+0

你需要的答案迴應,否則他們怎麼會知道你在找什麼? – Krishna

回答

1

你可以使用正則表達式類,然後做一個比較。

所以下面的例子中,你可以做到以下幾點:

var string1 = "<td>This is case-sensitive</td>"; 
var string2 = "<TD>This is case-Sensitive</TD>"; 

var regex = new Regex("<[^>]*>"); 

Console.WriteLine(regex.Replace(string1, "") == regex.Replace(string2, "")); 
//Result is False 
+0

謝謝!我會試試這個! – AngieM

+0

對不起,我對正則表達式不太流利,但是我怎樣才能在V形圖之間的字符串上做一個ToUpper或ToLower,而不是用「」替換它們?原因是我想要比較他們的內容(只是不是外殼)。在你的例子中,如果標籤不匹配,我將無法捕捉到。 – AngieM

+3

[使用正則表達式解析HTML的答案的必備鏈接](https://stackoverflow.com/questions/1732348/regex-match-open-tags-except-xhtml-self-contained-tags/1732454#1732454) –

-2
string str = "<td>This is case Sensitive</td>"; 
      str = Regex.Replace(str, "<.*?>", String.Empty); 
      Console.Write(str); 

這可能爲你工作

+0

你試過這個嗎?很明顯,這是行不通的。 – mason

+2

@mason我認爲它必須通用於所有標籤 – Krishna

+0

爲什麼選擇Downvote?通過刪除​​標籤,你可以比較兩個字符串 –