Parse a non-ascii (unicode) number-string as integer in .NET

前端 未结 3 1600
臣服心动
臣服心动 2021-02-20 06:49

I have a string containing a number in a non-ascii format e.g. unicode BENGALI DIGIT ONE (U+09E7) : \"১\"

How do I parse this as an integer in .NET?

相关标签:
3条回答
  • 2021-02-20 07:21

    It looks like this is not possible using built in functionality:

    The only Unicode digits that the .NET Framework parses as decimals are the ASCII digits 0 through 9, specified by the code values U+0030 through U+0039.

    ...

    The attempts to parse the Unicode code values for Fullwidth digits, Arabic-Indic digits, and Bengali digits fail and throw an exception.

    (emphasis mine)

    Very strange as CultureInfo("bn-BD").NumberFormat.NativeDigits does contain them.

    0 讨论(0)
  • 2021-02-20 07:37

    Having found this question while looking for a similar answer, but not finding any of the answers quite matched what I needed, I wrote the following as it treats signs okay, and is faster to fail if given a very long string. It does not though, ignore any grouping characters like ,, ', , though that could be easily added if someone wanted (I didn't):

    public static int ParseIntInternational(this string str)
    {
      int result = 0;
      bool neg = false;
      bool seekingSign = true; // Accept sign at beginning only.
      bool done = false; // Accept whitespace at beginning end or between sign and number.
                         // If we see whitespace once we've seen a number, we're "done" and
                         // further digits should fail.
      for(int i = 0; i != str.Length; ++i)
      {
        if(char.IsWhiteSpace(str, i))
        {
          if(!seekingSign)
            done = true;
        }
        else if(char.IsDigit(str, i))
        {
          if(done)
            throw new FormatException();
          seekingSign = false;
          result = checked(result * 10 + (int)char.GetNumericValue(str, i));
        }
        else if(seekingSign)
          switch(str[i])
          {
            case '﬩': case '+':
              //do nothing: Sign unchanged.
              break;
            case '-': case '−':
              neg = !neg;
              break;
            default:
              throw new FormatException();
          }
        else throw new FormatException();
      }
      if(seekingSign)
        throw new FormatException();
      return neg ? -result : result;
    }
    
    0 讨论(0)
  • 2021-02-20 07:38

    You could create a new string that is the same as the old string except the native digits are replaced with Latin decimal digits. This could be done reliably by looping through the characters and checking the value of char.IsDigit(char). If this function returns true, then convert it with char.GetNumericValue(char).ToString().

    Like this:

    static class DigitHelper
    {
        public static string ConvertNativeDigits(this string text)
        {
            if (text == null)
                return null;
            if (text.Length == 0)
                return string.Empty;
            StringBuilder sb = new StringBuilder();
            foreach (char character in text)
            {
                if (char.IsDigit(character))
                    sb.Append(char.GetNumericValue(character));
                else
                    sb.Append(character);
            }
            return sb.ToString();
        }
    }
    
    
    int value = int.Parse(bengaliNumber.ConvertNativeDigits());
    
    0 讨论(0)
提交回复
热议问题