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.
The replace() is an inbuilt method in Ruby which replaces the contents of the set with the contents of the given enumerable object and returns self. Parameters: The function accepts an enumerable object which to be replaced in the set.
Ruby allows part of a string to be modified through the use of the []= method. To use this method, simply pass through the string of characters to be replaced to the method and assign the new string.
gsub! is a String class method in Ruby which is used to return a copy of the given string with all occurrences of pattern substituted for the second argument. If no substitutions were performed, then it will return nil. If no block and no replacement is given, an enumerator is returned instead.
The sub() method replaces just the first instance of a string with another. Gsub meanwhile replaces all instances. Thus:Gsub is closest to a “replace string” method. Sub() is conceptually a “replace first string” method. Ruby program that compares sub, gsubvalue = "abc abc"
"15:40:32.81".gsub(/:|\./, "")
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With