我想更改保存在变量中的波斯数字,如下所示:
string Value="۱۰۳۶۷۵۱";
到
string Value="1036751";
我怎样才能使用像文化信息这样简单的方法来做到这一点?
我的示例代码是:
List<string> NERKHCOlist = new List<string>();
NERKHCOlist = ScrappingFunction(NERKHCO, NERKHCOlist);
int NERKHCO_Price = int.Parse(NERKHCOlist[0]);//NERKHCOlist[0]=۱۰۳۶۷۵۱
&lt; =所以它不能解析为int 这是在我的函数中,它返回列表项中包含波斯数字的列表
protected List<string> ScrappingFunction(string SiteAddress, List<string> NodesList)
{
string Price = "null";
List<string> Targets = new List<string>();
foreach (var path in NodesList)
{
HtmlNode node = document.DocumentNode.SelectSingleNode(path.ToString());//recognizing Target Node
Price = node.InnerHtml;//put text of target node in variable(PERSIAN DIGITS)
Targets.Add(Price);
}
return Targets;
}
答案 0 :(得分:19)
只需使用以下代码:
private string toPersianNumber(string input)
{
string[] persian = new string[10] { "۰", "۱", "۲", "۳", "۴", "۵", "۶", "۷", "۸", "۹" };
for (int j=0; j<persian.Length; j++)
input = input.Replace(persian[j], j.ToString());
return input;
}
答案 1 :(得分:10)
您需要首先使用例如解析它们具有正确文化说明符的Int32.Parse()。一旦你把它作为一个普通的整数,它只需要在它上面调用ToString(),再次使用正确的文化说明符。
另一种解决方案是逐个字符地处理字符串,并用相应的(west) arabic numeral替换任何波斯数字字符。如果需要,可以按原样保留其他字符。
如果字符串确实包含数字,则应使用整数解析方法。如果它不仅仅是一个数字,而是一个电话号码,序列号等,您可能需要使用替换算法。
答案 2 :(得分:10)
使用文化将数字从任何语言转换为任何语言
功能:
public static string ConvertDigitChar(this string str, CultureInfo source, CultureInfo destination)
{
for (int i = 0; i <= 9; i++)
{
str = str.Replace(source.NumberFormat.NativeDigits[i], destination.NumberFormat.NativeDigits[i]);
}
return str;
}
public static string ConvertDigitChar(this int digit, CultureInfo destination)
{
string res = digit.ToString();
for (int i = 0; i <= 9; i++)
{
res = res.Replace(i.ToString(), destination.NumberFormat.NativeDigits[i]);
}
return res;
}
如何使用这些功能:
var fa = CultureInfo.GetCultureInfoByIetfLanguageTag("fa");
var en = CultureInfo.GetCultureInfoByIetfLanguageTag("en");
string str = "۰0۱1۲2۳3۴4۵5۶6۷7۸8۹9";
string r1 = str.ConvertDigitChar(en, fa);
string r2 = str.ConvertDigitChar(fa, en);
int i = 123456789;
string r3 = i.ConvertDigitChar(fa);
结果:
r1:"۰۰۱۱۲۲۳۳۴۴۵۵۶۶۷۷۸۸۹۹"
r2:"00112233445566778899"
r3:"۰۱۲۳۴۵۶۷۸۹"
答案 3 :(得分:6)
您可以像这样手动转换它们
char[][] numbers = new char[][]
{
"0123456789".ToCharArray(),"persian numbers 0-9 here".ToCharArray()
};
public void Convert(string problem)
{
for (int x = 0; x <= 9; x++)
{
problem.Replace(numbers[0][x], numbers[1][x]);
}
}
我不知道波斯数字,所以你必须将它们添加到char数组中。
答案 4 :(得分:6)
我建议使用两种方法来解决此问题(我为每个方法创建一个extension method):
1.foreach和替换
public static class MyExtensions
{
public static string PersianToEnglish(this string persianStr)
{
Dictionary<char, char> LettersDictionary = new Dictionary<char, char>
{
['۰'] = '0',['۱'] = '1',['۲'] = '2',['۳'] = '3',['۴'] = '4',['۵'] = '5',['۶'] = '6',['۷'] = '7',['۸'] = '8',['۹'] = '9'
};
foreach (var item in persianStr)
{
persianStr = persianStr.Replace(item, LettersDictionary[item]);
}
return persianStr;
}
}
2.Dictionary.Aggregate
public static class MyExtensions
{
public static string PersianToEnglish(this string persianStr)
{
Dictionary<string, string> LettersDictionary = new Dictionary<string, string>
{
["۰"] = "0",["۱"] = "1",["۲"] = "2",["۳"] = "3",["۴"] = "4",["۵"] = "5",["۶"] = "6",["۷"] = "7",["۸"] = "8",["۹"] = "9"
};
return LettersDictionary.Aggregate(persianStr, (current, item) =>
current.Replace(item.Key, item.Value));
}
}
有关Dictionary.Aggregate的更多信息:Microsoft
用法:
string result = "۱۰۳۶۷۵۱".PersianToEnglish();
答案 5 :(得分:1)
我写了这个扩展方法,将字符串中的阿拉伯语和波斯语数字转换为拉丁语表示
public static class Extensions
{
public static string ConvertDigitsToLatin(this string s)
{
var sb = new StringBuilder();
for (int i = 0; i < s.Length; i++)
{
switch (s[i])
{
//Persian digits
case '\u06f0':
sb.Append('0');
break;
case '\u06f1':
sb.Append('1');
break;
case '\u06f2':
sb.Append('2');
break;
case '\u06f3':
sb.Append('3');
break;
case '\u06f4':
sb.Append('4');
break;
case '\u06f5':
sb.Append('5');
break;
case '\u06f6':
sb.Append('6');
break;
case '\u06f7':
sb.Append('7');
break;
case '\u06f8':
sb.Append('8');
break;
case '\u06f9':
sb.Append('9');
break;
//Arabic digits
case '\u0660':
sb.Append('0');
break;
case '\u0661':
sb.Append('1');
break;
case '\u0662':
sb.Append('2');
break;
case '\u0663':
sb.Append('3');
break;
case '\u0664':
sb.Append('4');
break;
case '\u0665':
sb.Append('5');
break;
case '\u0666':
sb.Append('6');
break;
case '\u0667':
sb.Append('7');
break;
case '\u0668':
sb.Append('8');
break;
case '\u0669':
sb.Append('9');
break;
default:
sb.Append(s[i]);
break;
}
}
return sb.ToString();
}
}
答案 6 :(得分:1)
这里我的代码将变量中的波斯数字转换为英语 ,通过扩展方法(表达后可以使用点)
private static readonly string[] pn = { "۰", "۱", "۲", "۳", "۴", "۵", "۶", "۷", "۸", "۹" };
private static readonly string[] en = { "0", "1", "2", "3", "4", "5", "6", "7", "8", "9" };
public static string ToEnglishNumber(this string strNum)
{
string chash = strNum;
for (int i = 0; i < 10; i++)
chash = chash.Replace(pn[i], en[i]);
return chash;
}
public static string ToEnglishNumber(this int intNum)
{
string chash = intNum.ToString();
for (int i = 0; i < 10; i++)
chash = chash.Replace(pn[i], en[i]);
return chash;
}
当你想使用这段代码时必须写: txt1.Value.ToEnglishNumber();
答案 7 :(得分:1)
Saeed的解决方案没问题,但对于双变量,您还必须将“。”字符替换为“。” ,所以你可以使用:
private string ToEnglishNumber(string strNum)
{
string[] pn = { "۰", "۱", "۲", "۳", "۴", "۵", "۶", "۷", "۸", "۹", "٫" };
string[] en = { "0", "1", "2", "3", "4", "5", "6", "7", "8", "9","." };
string chash = strNum;
for (int i = 0; i < 11; i++)
chash = chash.Replace(pn[i], en[i]);
return chash;
}
答案 8 :(得分:1)
public static string ChangeNumberToEnglishNumber(string value)
{
string result=string.Empty;
foreach (char ch in value)
{
try
{
double convertedChar = char.GetNumericValue(ch);
if (convertedChar >= 0 && convertedChar <= 9)
{
result += convertedChar.ToString(CultureInfo.InvariantCulture);
}
else
{
result += ch;
}
}
catch (Exception e)
{
result += ch;
}
}
return result;
}
答案 9 :(得分:0)
您可以使用Windows.Globalization.NumberFormatting.DecimalFormatter类来解析字符串。这将解析任何支持的数字系统中的字符串(只要它在内部是连贯的)。
答案 10 :(得分:0)
使用此静态类可以轻松更改规范化数字:
public static class Numbers
{
public static string ChangeToEnglishNumber(this string text)
{
var englishNumbers = string.Empty;
for (var i = 0; i < text.Length; i++)
{
if(char.IsNumber(text[i])) englishNumbers += char.GetNumericValue(text, i);
else englishNumbers += text[i];
}
return englishNumbers;
}
}
样品:
string test = "۱۰۳۶۷۵۱".ChangeToEnglishNumber(); // => 1036751
答案 11 :(得分:0)
public static string ToEnglishNumber(string input)
{
var englishnumbers = new Dictionary<string, string>()
{
{"۰","0" }, {"۱","1" }, {"۲","2" }, {"۳","3" },{"۴","4" }, {"۵","5" },{"۶","6" }, {"۷","7" },{"۸","8" }, {"۹","9" },
{"٠","0" }, {"١","1" }, {"٢","2" }, {"٣","3" },{"٤","4" }, {"٥","5" },{"٦","6" }, {"٧","7" },{"٨","8" }, {"٩","9" },
};
foreach (var numbers in englishnumbers)
input = input.Replace(numbers.Key, numbers.Value);
return input;
}
答案 12 :(得分:0)
简洁明了:
public static class Utility
{
// '۰' = 1632
// '0' = 48
// ------------
// 1632 => '۰'
//- 1584
//--------
// 48 => '0'
public static string GetEnglish(this string input)
{
char[] persianDigitsAscii = input.ToCharArray(); //{ 1632, 1633, 1634, 1635, 1636, 1637, 1638, 1639, 1640, 1641 };
string output = "";
for (int k = 0; k < persianDigitsAscii.Length; k++)
{
persianDigitsAscii[k] = (char) (persianDigitsAscii[k] - 1584);
output += persianDigitsAscii[k];
}
return output;
}
}
答案 13 :(得分:0)
使用此扩展名,也用于阿拉伯语键盘,例如:“ ۵”,“ ٥”或“ ۴”,“ ٤”
static char[][] persianChars = new char[][]
{
"0123456789".ToCharArray(),"۰۱۲۳۴۵۶۷۸۹".ToCharArray()
};
static char[][] arabicChars = new char[][]
{
"0123456789".ToCharArray(),"٠١٢٣٤٥٦٧٨٩".ToCharArray()
};
public static string toPrevalentDigits(this string src)
{
if (string.IsNullOrEmpty(src)) return null;
for (int x = 0; x <= 9; x++)
{
src = src.Replace(persianChars[1][x], persianChars[0][x]);
}
for (int x = 0; x <= 9; x++)
{
src = src.Replace(arabicChars[1][x], arabicChars[0][x]);
}
return src;
}
答案 14 :(得分:0)
有一种简单的方法
public static string Fa2En(string str)
{
return str.Replace("۰", "0")
.Replace("۱", "1")
.Replace("۲", "2")
.Replace("۳", "3")
.Replace("۴", "4")
.Replace("۵", "5")
.Replace("۶", "6")
.Replace("۷", "7")
.Replace("۸", "8")
.Replace("۹", "9");
}
答案 15 :(得分:0)
我最近将此代码用于非波斯字符的 10 条记录,但原因是处理缓慢,因此您必须在替换之前使用 LINQ 进行检查
static Dictionary<char, char> LettersDictionary = new Dictionary<char, char>() {
{'۰', '0'},
{'۱', '1'},
{'۲', '2'},
{'۳', '3'},
{'۴', '4'},
{'۵', '5'},
{'۶', '6'},
{'۷', '7'},
{'۸', '8'},
{'۹', '9'}
};
public static string PersianToEnglish(this string persianStr)
{
if (persianStr.Any(x => LettersDictionary.Keys.Contains(x)))
{
foreach (var item in persianStr)
{
try { persianStr = persianStr.Replace(item, LettersDictionary[item]); }
catch { }
}
}
return persianStr;
}