HOW-TO: get a number from a string.
 

News:

29 December 2022 - PtokaX 0.5.3.0 (20th anniversary edition) released...
11 April 2017 - PtokaX 0.5.2.2 released...
8 April 2015 Anti child and anti pedo pr0n scripts are not allowed anymore on this board!
28 September 2015 - PtokaX 0.5.2.1 for Windows 10 IoT released...
3 September 2015 - PtokaX 0.5.2.1 released...
16 August 2015 - PtokaX 0.5.2.0 released...
1 August 2015 - Crowdfunding for ADC protocol support in PtokaX ended. Clearly nobody want ADC support...
30 June 2015 - PtokaX 0.5.1.0 released...
30 April 2015 Crowdfunding for ADC protocol support in PtokaX
26 April 2015 New support hub!
20 February 2015 - PtokaX 0.5.0.3 released...
13 April 2014 - PtokaX 0.5.0.2 released...
23 March 2014 - PtokaX testing version 0.5.0.1 build 454 is available.
04 March 2014 - PtokaX.org sites were temporary down because of DDOS attacks and issues with hosting service provider.

Main Menu

HOW-TO: get a number from a string.

Started by plop, 07 June, 2004, 16:22:25

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

plop

made this 1 for the fun, but it's actualy a nice example how to grab a number from a string in multiple way's.
string = "  12.4 GB "

-- strfind with magic
s,e,num = strfind(string, "([%d%.]+)") 
print(num)

-- strfind with simple pattern
s,e,num = strfind(string, "(%d+%.%d)") 
print(num)

-- strsub
num = strsub(string, 3, (strlen(string)-3) )
print(num)

-- strfind + strsub 
a,b = strfind(string, "^%D+(%d)")
c,d = strfind(string, "(%d)%D+$")
num = strsub(string,b,c)
print(num)

-- gsub
num = gsub(string, "[^%d%.]","")
print(num)

-- complex gsub
function replace(t)
   if tonumber(t) then
      return t
   elseif t == "." then
      return t
   else
      return ""
   end
end
num = gsub(string, "(.)",function(t) return replace(t) end)
print(num)
if you know more way's pls post them.

plop
http://www.plop.nl lua scripts/howto\'s.
http://www.thegoldenangel.net
http://www.vikingshub.com
http://www.lua.org

>>----> he who fights hatred with hatred, drives the spreading of hatred <----<<

NotRabidWombat

Good job.

-NotRabidWombat


I like childish behavior. Maybe this post will be deleted next.

Herodes

Very good stuff ..
I think it can be usefull to have the actual result for each of those .. :)

plop

QuoteOriginally posted by Herodes
Very good stuff ..
I think it can be usefull to have the actual result for each of those .. :)
execute the script with the lua command line.
you'll they all have the exact same result, just some take the long way home. lol

@both, thx for the compliments.

plop
http://www.plop.nl lua scripts/howto\'s.
http://www.thegoldenangel.net
http://www.vikingshub.com
http://www.lua.org

>>----> he who fights hatred with hatred, drives the spreading of hatred <----<<

SMF spam blocked by CleanTalk