如何在 LUA 中从字符串中获取数字?

我有一个字符串 "0,0,0,-58.43083113,,"

怎么样才能用 LUA 将其中的 4 个数字都作为双精度数获取到呢?谢谢!

我已经试过使用 string.match(),但是它没有起作用。

原文链接 https://stackoverflow.com/questions/70036720

点赞
stackoverflow用户2858170
stackoverflow用户2858170
```lua
local text = "0,0,0,-58.43083113,,"

local numbers = {}
text:gsub("[^,]+", function (str) table.insert(numbers, tonumber(str)+.0) end)
print(table.concat(numbers, ", "))

或者

for str in text:gmatch("[^,]+") do
  table.insert(numbers, tonumber(str) + .0)
end

当然,这假设你的字符串中只包含数字表示和逗号。 ```

2021-11-19 15:03:21