Checking if string is numeric in dart

后端 未结 4 1257
悲哀的现实
悲哀的现实 2021-02-02 06:59

I need to find out if a string is numeric in dart. It needs to return true on any valid number type in dart. So far, my solution is



        
4条回答
  •  故里飘歌
    2021-02-02 07:13

    Even shorter. Despite the fact it will works with double as well, using num is more accurately.

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

    num.tryParse inside:

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

提交回复
热议问题