Detect if string contains a float?

时光毁灭记忆、已成空白 提交于 2019-12-07 15:15:30

I would use TryStrToFloat():

if TryStrToFloat(str, value, FormatSettings) then
  ....

If you are prepared to use the default system wide format settings then you can omit the final parameter:

if TryStrToFloat(str, value) then
  ....

Can you use a RegEx here? Something like:

([+-]?[0-9]+(?:\.[0-9]*)?) 
Leonardo Herrera

The problem with this question is that saying "is too slow" doesn't tell much. What does the profiler tells to you? Do you have an informed idea about the input data? What about different notations, for example, 6.02e23?

If your input data is mostly noise, then using regular expressions (as answered here) may improve things but only as a first filter. You could then add a second step to actually obtain your number, as explained by David's answer.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!