I have the following output:
time = 15:40:32.81
and I want to eliminate : and the . so that it looks like this:
15403281
I tried doing a time.gsub(/\:\s/'')
but that didn't work.
Thanks for your help in advance
I have the following output:
time = 15:40:32.81
and I want to eliminate : and the . so that it looks like this:
15403281
I tried doing a time.gsub(/\:\s/'')
but that didn't work.
Thanks for your help in advance
time = '15:40:32.81'
numeric_time = time.gsub(/[^0-9]+/, '')
# numeric_time will be 15403281
[^0-9]
specifies a character class containing any character which is not a digit (^
at the beginning of a class negates it), which will then be replaced by an empty string (or, in other words, removed).
(Updated to replace \d
with 0-9
for clarity, though they are equivalent).
If you want to be fancy and use an actual time object...
time = Time.now
time.strftime("%H%M%S") + time.usec.to_s[0,2]
# returns "15151788"
time.delete ':.'
But it'll edit your variable. If you don't want it:
time.dup.delete ':.'