检查dart中字符串是否为数字

scr*_*rd3 19 string numbers isnumeric dart

我需要找出dart中的字符串是否为数字.它需要在dart中的任何有效数字类型上返回true.到目前为止,我的解决方案是

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

是否有本地方式来做到这一点?如果没有,有没有更好的方法呢?

小智 22

在Dart 2中,不推荐使用此方法

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

相反,使用

 bool _isNumeric(String str) {
    if(str == null) {
      return false;
    }
    return double.tryParse(str) != null;
  }
Run Code Online (Sandbox Code Playgroud)


Gün*_*uer 19

这可以简化一点

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;
}
Run Code Online (Sandbox Code Playgroud)
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'
Run Code Online (Sandbox Code Playgroud)

来自double.parse DartDoc

   * Examples of accepted strings:
   *
   *     "3.14"
   *     "  3.14 \xA0"
   *     "0."
   *     ".0"
   *     "-1.e3"
   *     "1234E+7"
   *     "+.12e-9"
   *     "-NaN"
Run Code Online (Sandbox Code Playgroud)

此版本也接受十六进制数字

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'));
Run Code Online (Sandbox Code Playgroud)

真正

  • 由于问题没有定义数字的含义,这绝对是一个解决方案,但请注意,它也接受“Infinity”和“NaN”。是否允许使用开头的“-”还取决于确切的定义。如果它还必须接受“0x123”,则可以使用“num.parse”而不是“double.parse”。 (2认同)

小智 14

if (int.tryParse(value) == null) {
  return 'Only Number are allowed';
}
Run Code Online (Sandbox Code Playgroud)


Nel*_*ora 11

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

RegExp _numeric = RegExp(r'^-?[0-9]+$');

/// check if the string contains only numbers
 bool isNumeric(String str) {
return _numeric.hasMatch(str);
}
Run Code Online (Sandbox Code Playgroud)


Air*_*ark 5

甚至更短。尽管事实上它也可以使用double,但使用起来num更准确。

isNumeric(string) => num.tryParse(string) != null;
Run Code Online (Sandbox Code Playgroud)

num.tryParse 内:

static num tryParse(String input) {
  String source = input.trim();
  return int.tryParse(source) ?? double.tryParse(source);
}
Run Code Online (Sandbox Code Playgroud)


Luc*_*lla 5


是的,有一种更“复杂”的方式*rsrs*。

extension Numeric on String {
  bool get isNumeric => num.tryParse(this) != null ? true : false;
}

main() {
  print("1".isNumeric); // true
  print("-1".isNumeric); // true
  print("2.5".isNumeric); // true
  print("-2.5".isNumeric); // true
  print("0x14f".isNumeric); // true
  print("2,5".isNumeric); // false
  print("2a".isNumeric); // false
}
Run Code Online (Sandbox Code Playgroud)