您可以使用正则表达式查找“>”之间的HTML部分和'
// Convert all English digits in a string to Arabic digit equivalents
public static string ToArabicNums(string src)
{
const string digits = "۰۱۲۳۴۵۶۷۸۹";
return string.Join("",
src.Select(c => c >= '0' && c <= '9' ? digits[((int)c - (int)'0')] : c)
);
}
// Convert all English digits in the text segments of an HTML
// document to Arabic digit equivalents
public static string ToArabicNumsHtml(string src)
{
string res = src;
Regex re = new Regex(@">(.*?)
// get Regex matches
MatchCollection matches = re.Matches(res);
// process in reverse in case transformation function returns
// a string of a different length
for (int i = matches.Count - 1; i >= 0; --i)
{
Match nxt = matches[i];
if (nxt.Groups.Count == 2 && nxt.Groups[1].Length > 0)
{
Group g = nxt.Groups[1];
res = res.Substring(0, g.Index) + ToArabicNums(g.Value) +
res.Substring(g.Index + g.Length);
}
return res;
}这并不完美,因为它根本不检查标记之外的HTML字符说明符,例如构造;(۱表示1等)以通过Unicode值指定字符,并将替换这些中的数字。它也不会在第一个标签之前或最后一个标签之后处理任何额外的文本。
样品:
Calling: ToArabicNumsHtml("
I was born in 1988
")Result: "