I am trying to convert the plain text Arabic Numerals into Eastern Arabic digits. So basically taking 1 2 3... and converting them into ١ ٢ ٣.... The function converts all numbers, including any numbers contained within tags, i.e. H1
.
private void LoadHtmlFile(object sender, EventArgs e)
{
var htmlfile = "<html><body><h1>i was born in 1988</h1></body></html>".ToArabicNumber(); ;
webBrowser1.DocumentText=htmlfile;
}
}
public static class StringHelper
{
public static string ToArabicNumber(this string str)
{
if (string.IsNullOrEmpty(str)) return "";
char[] chars;
chars = str.ToCharArray();
for (int i = 0; i < str.Length; i++)
{
if (str[i] >= '0' && str[i] <= '9')
{
chars[i] += (char)1728;
}
}
return new string(chars);
}
}
I also tried targeting only numbers in InnerText, but it also did not work. The code below changes tag numbers as well.
private void LoadHtmlFile(object sender, EventArgs e)
{
var htmlfile = "<html><body><h1>i was born in 1988</h1></body></html>" ;
webBrowser1.DocumentText=htmlfile;
}
private void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
{
webBrowser1.Document.Body.InnerText = webBrowser1.Document.Body.InnerText.ToArabicNumber();
}
Any suggestions?
Use regular expressions. Here is the JavaScript code I myself use:
To make sure, you only convert numbers, you can use a better regular expression: \b[0-9]+\b
Just add this at the end of your document, it will works fine :-)
This function can convert English to Persian , Arabic and ordu
You can use a regular expression to find the parts of the HTML that are between '>' and '<' characters, and operate on those. This will prevent the code from processing the tag names and attributes (style, etc).
This isn't perfect, since it doesn't check at all for HTML character specifiers outside of the tags, such as the construct
&#<digits>;
(۱
for ۱, etc)to specify a character by Unicode value, and will replace the digits in these. It also won't process any extra text before the first tag or after the last tag.Sample:
Use whatever code you prefer in
ToArabicNums
to do the actual transformation, or generalize it by passing in a transformation function.