【问题标题】:Checking if string is numeric in dart检查飞镖中的字符串是否为数字
【发布时间】:2014-07-27 22:24:31
【问题描述】:

我需要确定一个字符串在 dart 中是否为数字。它需要对 dart 中的任何有效数字类型返回 true。到目前为止,我的解决方案是

bool isNumeric(String str) {
  try{
    var value = double.parse(str);
  } on FormatException {
    return false;
  } finally {
    return true;
  }
}

有没有本地方法可以做到这一点?如果没有,有更好的方法吗?

【问题讨论】:

    标签: string numbers dart isnumeric


    【解决方案1】:
    if (int.tryParse(value) == null) {
      return 'Only Number are allowed';
    }
    

    【讨论】:

      【解决方案2】:

      对于任何想要使用正则表达式的非本地方式的人

      RegExp _numeric = RegExp(r'^-?[0-9]+$');
      
      /// check if the string contains only numbers
       bool isNumeric(String str) {
      return _numeric.hasMatch(str);
      }
      

      【讨论】:

      • 这是最容易申请的。我建议将 _numeric 放在函数中,因为这样它应该是 plugAndPlay。结果应该是: bool isNumeric(String str) { RegExp _numeric = RegExp(r'^-?[0-9]+$');返回 _numeric.hasMatch(str); }
      【解决方案3】:

      这可以简化一点

      void main(args) {
        print(isNumeric(null));
        print(isNumeric(''));
        print(isNumeric('x'));
        print(isNumeric('123x'));
        print(isNumeric('123'));
        print(isNumeric('+123'));
        print(isNumeric('123.456'));
        print(isNumeric('1,234.567'));
        print(isNumeric('1.234,567'));
        print(isNumeric('-123'));
        print(isNumeric('INFINITY'));
        print(isNumeric(double.INFINITY.toString())); // 'Infinity'
        print(isNumeric(double.NAN.toString()));
        print(isNumeric('0x123'));
      }
      
      bool isNumeric(String s) {
        if(s == null) {
          return false;
        }
        return double.parse(s, (e) => null) != null;
      }
      
      false   // null  
      false   // ''  
      false   // 'x'  
      false   // '123x'  
      true    // '123'  
      true    // '+123'
      true    // '123.456'  
      false   // '1,234.567'  
      false   // '1.234,567' (would be a valid number in Austria/Germany/...)
      true    // '-123'  
      false   // 'INFINITY'  
      true    // double.INFINITY.toString()
      true    // double.NAN.toString()
      false   // '0x123'
      

      来自 double.parse DartDoc

         * Examples of accepted strings:
         *
         *     "3.14"
         *     "  3.14 \xA0"
         *     "0."
         *     ".0"
         *     "-1.e3"
         *     "1234E+7"
         *     "+.12e-9"
         *     "-NaN"
      

      这个版本也接受十六进制数字

      bool isNumeric(String s) {
        if(s == null) {
          return false;
        }
      
        // TODO according to DartDoc num.parse() includes both (double.parse and int.parse)
        return double.parse(s, (e) => null) != null || 
            int.parse(s, onError: (e) => null) != null;
      }
      
      print(int.parse('0xab'));
      

      是的

      更新

      由于{onError(String source)} 现在已弃用,您可以只使用tryParse

      bool isNumeric(String s) {
       if (s == null) {
         return false;
       }
       return double.tryParse(s) != null;
      }
      

      【讨论】:

      • 由于问题没有定义数字的含义,这绝对是一个解决方案,但请注意它也会接受"Infinity""NaN"。是否应该允许初始的“-”也取决于确切的定义。如果它还必须接受"0x123",则可以使用num.parse 而不是double.parse
      • 感谢您的提示!我考虑过问什么应该被认为是一个数字。你当然是对的。有趣的是,"INFINITY" 不是数字,但 "NaN" 是。
      • 它只接受double.INFINITY.toString() 的输出,即“Infinity”(大写,但不是大写)。
      • 谢谢!这绝对是比我更好的解决方案。
      • 一个班轮:bool isNumeric(String s) => s != null && double.tryParse(s) != null;
      【解决方案4】:

      甚至更短。尽管它也适用于double,但使用num 更准确。

      isNumeric(string) => num.tryParse(string) != null;
      

      num.tryParse 内:

      static num tryParse(String input) {
        String source = input.trim();
        return int.tryParse(source) ?? double.tryParse(source);
      }
      

      【讨论】:

        【解决方案5】:

        在 Dart 2 中,该方法已被弃用

        int.parse(s, onError: (e) => null)

        改为使用

         bool _isNumeric(String str) {
            if(str == null) {
              return false;
            }
            return double.tryParse(str) != null;
          }
        

        【讨论】:

        • 此方法已弃用,请改用tryParse
        猜你喜欢
        • 2020-04-14
        • 1970-01-01
        • 1970-01-01
        • 2017-11-28
        • 2022-11-24
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多