[英]How replace whitespaces (unicode to utf-8) with a regex C#
我正在嘗試在C#中執行替換正則表達式。 我嘗試編寫的方法用UTF-8中的普通空格替換了一些Unicode字符(空格)。
讓我用代碼解釋。 我不好寫正則表達式,文化信息和正則表達式。
//This method replace white spaces in unicode by whitespaces UTF-8
public static string cleanUnicodeSpaces(string value)
{
//This first pattern works but, remove other special characteres
//For example: mark accents
//string pattern = @"[^\u0000-\u007F]+";
string cleaned = "";
string pattern = @"[^\u0020\u0009\u000D]+"; //Unicode characters
string replacement = ""; //Replace by UTF-8 space
Regex regex = new Regex(pattern);
cleaned = regex.Replace(value, replacement).Trim(); //Trim by quit spaces
return cleaned;
}
Unicode空格
我做錯了什么?
資源
解決方案感謝@wiktor-stribiżew和@ mathias-r-jessen,解決方案:
string pattern = @"[\u0020\u0009\u000D\u00A0]+";
//I include \u00A0 for replace  
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.