2017-04-11 134 views
0

我正在從數據庫中讀取字段並在GridView中顯示,並在該字段中包含文本中的<br/>標記。所以我試圖從代碼中刪除這些,但是當我檢查e.Row.Cells[index].Text的值時,它不包含<br/>並且代之以具有;br/&gt;從字符串中刪除html標記

所以,我試圖創建一個功能,消除開始<>結束或&開始,以;結尾的字符串。該代碼刪除<>,但仍表現出br/

代碼:

index = gv.Columns.HeaderIndex("Message"); 
if (index > 0) 
{ 
    string message = RemoveHTMLMarkup(e.Row.Cells[index].Text); 
    e.Row.Cells[index].Text = message; 
} 

static string RemoveHTMLMarkup(string text) 
{ 
     return Regex.Replace(Regex.Replace(text, "<.+?>", string.Empty), "&.+?;", string.Empty); 
} 

如何刪除<br/>標籤?

+2

[義務鏈接](http://stackoverflow.com/a/1732454/2307070)關於爲什麼ṫ̨̗̺̭̮̞̗̜̮̗̙̫̺̖̭̯͊ͨ̌͒̍͘͘͟͝h̸͓̩̙͙̻̗͔̞̘̟̩̯͋͑͐a̴̧ͨͭ͒ͯ̓͐̇ͥ͢҉̨̳̜̤͍͖t̵̳̳͕͉͋̓͐ͦͬ̈̚是一個壞主意。另外,你確定它是'br/>'而不是'< br/>'? –

+0

@ThomasAyoub是的,你是對的,它是'< br/>'。所以,如果我不使用正則表達式,那麼我應該使用什麼? – user123456789

回答

3

由於這是一個字符串,你(SH | C)烏爾德只使用String.Replace()

static string RemoveHTMLNewLines(string text) 
{ 
    return text.Replace("&lt;br/&gt;", string.Empty); 
} 

或者,如果有需要更換Environment.NewLine