I suggest two approaches to handle this issue(I Create an extension method for each of them):
1.foreach and replace
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));
}
}
More info about Dictionary.Aggregate: Microsoft
Usage:
string result = "???????".PersianToEnglish();
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…