C# 带多组分隔符的Int32.TryParse

C# 带多组分隔符的Int32.TryParse,c#,.net,C#,.net,我正在尝试创建一个方法,可以在ok输入中处理和验证整数。问题在于我们的要求,即无论选择何种语言,以下数字均为ok输入: 1500, 1.500, 1,500, 1 500 -1500, -1.500, -1,500, -1 500 1500000, 1.500.500, 1,500,500 1 500 500 -1500000, -1.500.500, -1,500,500 -1 500 500 等等 我的方法现在如下所示: private bool TryParseInteger(strin

我正在尝试创建一个方法,可以在ok输入中处理和验证整数。问题在于我们的要求,即无论选择何种语言,以下数字均为ok输入:

1500, 1.500, 1,500, 1 500
-1500, -1.500, -1,500, -1 500
1500000, 1.500.500, 1,500,500 1 500 500
-1500000, -1.500.500, -1,500,500 -1 500 500
等等

我的方法现在如下所示:

private bool TryParseInteger(string controlValue, out int controlInt)
{
      int number;
      NumberStyles styles = NumberStyles.Integer | NumberStyles.AllowThousands;

      bool result = Int32.TryParse(controlValue, styles,
            CultureInfo.InvariantCulture, out number);
      controlInt = number;

      return result;
}
controlValue = new string(controlValue.Where(c => !char.IsPunctuation(c) && c != ' ' || c == '-'));
private bool TryParseInteger(string controlValue, out int controlInt)
{
      int number;
      NumberStyles styles = NumberStyles.Integer | NumberStyles.AllowThousands;

      bool result = Int32.TryParse(controlValue, styles,
            CultureInfo.InvariantCulture, out number);

      if (!result)
      {
            controlValue = controlValue.Replace('.', ',');
            result = Int32.TryParse(controlValue, styles,
                  CultureInfo.InvariantCulture, out number);
      }
      controlInt = number;

      return result;
}
这不是我想要的。1.500和1.500.500未验证为正确输入

还有别的办法吗


谢谢你的帮助。结果是1.500,50(等等)不应该通过验证,这使得建议的解决方案不起作用。还有其他想法吗?

我认为这里的问题是每个国家对
1.500
的理解不同。例如,在美国,它等于1.5,有一些无意义的零,而在德国(如果记忆有用的话),它被理解为一千五百

与此相协调,您应该在方法的开头添加一行,如下所示:

private bool TryParseInteger(string controlValue, out int controlInt)
{
      int number;
      NumberStyles styles = NumberStyles.Integer | NumberStyles.AllowThousands;

      bool result = Int32.TryParse(controlValue, styles,
            CultureInfo.InvariantCulture, out number);
      controlInt = number;

      return result;
}
controlValue = new string(controlValue.Where(c => !char.IsPunctuation(c) && c != ' ' || c == '-'));
private bool TryParseInteger(string controlValue, out int controlInt)
{
      int number;
      NumberStyles styles = NumberStyles.Integer | NumberStyles.AllowThousands;

      bool result = Int32.TryParse(controlValue, styles,
            CultureInfo.InvariantCulture, out number);

      if (!result)
      {
            controlValue = controlValue.Replace('.', ',');
            result = Int32.TryParse(controlValue, styles,
                  CultureInfo.InvariantCulture, out number);
      }
      controlInt = number;

      return result;
}

这将删除所有逗号、句点和空格,因为您只需要整数。如果你想得到一个小数点,那么我们就有麻烦了……

只要把那些符号都换掉就行了

此代码:

string[] arr = new[] { "1500", "1.500", "1,500", "1 500", "-1500", "-1.500", "-1,500", "-1 500",
                                "1500000", "1.500.500", "1,500,500","1 500 500",
                                "-1500000", "-1.500.500", "-1,500,500","-1 500 500"};

foreach (var s in arr)
{
    int i = int.Parse(s.Replace(" ", "").Replace(",", "").Replace(".", ""));

    Console.WriteLine(i);
}
产生:

一千五百

一千五百

一千五百

一千五百

-一千五百

-一千五百

-一千五百

-一千五百

150万

1500500

1500500

1500500

-150万

-1500500

-1500500

-1500500


如果不想匹配
1500.500
1.500500
,可以将所有
替换为
,然后再次尝试解析它。大概是这样的:

private bool TryParseInteger(string controlValue, out int controlInt)
{
      int number;
      NumberStyles styles = NumberStyles.Integer | NumberStyles.AllowThousands;

      bool result = Int32.TryParse(controlValue, styles,
            CultureInfo.InvariantCulture, out number);
      controlInt = number;

      return result;
}
controlValue = new string(controlValue.Where(c => !char.IsPunctuation(c) && c != ' ' || c == '-'));
private bool TryParseInteger(string controlValue, out int controlInt)
{
      int number;
      NumberStyles styles = NumberStyles.Integer | NumberStyles.AllowThousands;

      bool result = Int32.TryParse(controlValue, styles,
            CultureInfo.InvariantCulture, out number);

      if (!result)
      {
            controlValue = controlValue.Replace('.', ',');
            result = Int32.TryParse(controlValue, styles,
                  CultureInfo.InvariantCulture, out number);
      }
      controlInt = number;

      return result;
}

您可以创建自己的自定义区域性,并根据需要更改
NumberGroupSeparator

    private static bool TryParseInteger(string controlValue, out int controlInt)
    {
        String[] groupSeparators = { ",", ".", " "};
        CultureInfo customCulture = CultureInfo.InvariantCulture.Clone() as CultureInfo;
        customCulture.NumberFormat.NumberDecimalSeparator = "SomeUnlikelyString";

        NumberStyles styles = NumberStyles.Integer | NumberStyles.AllowThousands;

        bool success = false;
        controlInt = 0;
        foreach (var separator in groupSeparators)
        {
            customCulture.NumberFormat.NumberGroupSeparator = separator;
            success = Int32.TryParse(controlValue, styles, customCulture, out controlInt);

            if (success)
            {
                break;
            }
        }

        return success;
    }

我的第一种方法是当面称需求的作者为白痴。以更严肃的方式;如果对
CultureInfo.InvariantCulture
的转换失败,请使用另一个使用
作为千位分隔符的区域性(如丹麦区域性)重试。您希望同时允许十进制分隔符也作为千位分隔符。那是行不通的。您可以尝试分析两次,首先使用
不变量区域性
,然后使用具有数千个分隔符的
,但是如果人们想要输入小数,会发生什么情况?1.5是双精度值,您应该有一个字符串.replace方法来删除。和,然后传递值如何判断
1.567
是大约1500还是大约1.5?如果确定只处理整数,可以
string。替换
所有空格、点和逗号,有效地删除它们,然后尝试解析。大问题。在末尾添加一个
| | c='-'
将修复它。@Scott如果我使用
,它将返回false!字符ispunchuation
。我实际上从我当前的项目中删除了它(顺便说一句,它工作得很好),并将变量名更改为
controlValue
:)问题是你需要
!char.ispunchuation('-')
返回
true
。当前实现会导致负数变为正数。要么你当前的项目不支持负数,要么我刚刚为你发现了一个没有人报告的bug。啊,我明白你的意思了。该项目只有一名维护人员(me)和一名设计师,我要求它不能使用负数。我从中提取的部分实际上将
字符串
拆分为
字符串
uint
。有一天我会用一个普通的'ol
int
替换
uint
,但它的优先级较低。谢谢你发现了这个,它已经被编辑过了。