Most elegant way to detect if a String is a number?

前端 未结 11 808

Is there a better, more elegant (and/or possibly faster) way than

boolean isNumber = false;
try{
   Double.valueOf(myNumber);
   isNumber = true;
} catch (Nu         


        
11条回答
  •  -上瘾入骨i
    2020-12-30 12:00

    If you want something that's blisteringly fast, and you have a very clear idea of what formats you want to accept, you can build a state machine DFA by hand. This is essentially how regexes work under the hood anyway, but you can avoid the regex compilation step this way, and it may well be faster than a generic regex compiler.

提交回复
热议问题