4

I was looking for some RegEx or any other method to convert digits 0-9 into ०-९ (devanagari script). I am using asp.net but could not find any method in globalization namespace which does this.

Any help is greatly appreciated.

casperOne
  • 73,706
  • 19
  • 184
  • 253

2 Answers2

1

Found similar post here!

My solution is bit different though as I know the source and destination culture. So I can hard-code the digits array.

        string devYear = "";
        string[] devD = { "०", "१", "२", "३", "४", "५", "६", "७", "८", "९" };
        char[] digits = curYear.ToCharArray();
        foreach (char ch in digits)
        {
            devYear += devD[int.Parse(ch.ToString())];
        }

Another change is that I am iterating through the year digits instead of devD array. Saves few iterations as most numbers will be less than 10 digit. In my case, only four digits.

Hopefully will be useful for someone stuck up on similar lines.

Community
  • 1
  • 1
0

Does each latin digit 0..9 map to exactly a devanagari digit (I do think so, if I understand Wikipedia correctly)=

If yes, how about the following:

public static string ConvertDigits( string s )
{
    return s
        .Replace("0", "०")
        .Replace("1", "१")
        .Replace("2", "२")
        .Replace("3", "३")
        .Replace("4", "४")
        .Replace("5", "५")
        .Replace("6", "६")
        .Replace("7", "७")
        .Replace("8", "८")
        .Replace("9", "९");
}

For optimization, you could check for string.IsNullOrEmpty() before calling the string.Replace function.

In Addition (if this is suitable for a devanagari digit), call the string.Replace() function overload that takes chars as parameters rather than strings.

Uwe Keim
  • 39,551
  • 56
  • 175
  • 291
  • 2
    Historically, each latin digit in decimal number system was taken from devanagari digits so yes, it does map back correctly :) This is a pretty simple solution but not quite elegant. What I was looking for is something similar to the translate() function used in xslt http://www.w3schools.com/xpath/xpath_functions.asp#string. Also, if I am not mistaken, we should use stringbuilder instead of string for better results. Thanks anyway ! – Rohit Sahasrabudhe Jul 12 '11 at 16:16