Ruby:删除(包括)十进制之后的所有内容

时间:2022-06-29 20:23:35

I've got this string: "37:48.1234567" (there's a colon and there's a decimal in there)

我有这个字符串:“37:48.1234567”(有一个冒号,那里有一个小数)

I need to remove everything from the decimal and past it.

我需要从小数中删除所有内容并将其删除。

The number before the decimal could be any length (ie. 1:23:39.12357).

小数点前的数字可以是任意长度(即1:23:39.12357)。

3 个解决方案

#1


13  

str.split(".")[0]

Hope that helps!

希望有所帮助!

#2


4  

I made a little benchmark with the solutions up to now. I modified the solutions a bit.

到目前为止,我对解决方案做了一些基准测试。我稍微修改了一下解决方案。

One unclear problem: Whats the result for 1:23:39.123.57? 1:23:39 or 1:23:39.123?

一个不明确的问题:结果是1:23:39.123.57? 1:23:39或1:23:39.123?

I choosed 1:23:39, only test regex has the other solution (see Tims answer)

我选择了1:23:39,只有测试正则表达式有另一种解决方案(见Tims回答)

The splitvariant has 2x2 variants: [0] or .first and split with a 2nd parameter , 2. The 2nd parameter of split limit the number of the splitted elements. We need only the first, so we may split in first and rest.

splitvariant具有2x2变体:[0]或.first并使用第二个参数2进行分割。分割的第二个参数限制分割元素的数量。我们只需要第一个,所以我们可以先分开休息。

The fastes solution: split('.', 2). If you use first or [0] makes no difference. I would prefer first.

紧固解决方案:拆分('。',2)。如果你先使用或[0]没有区别。我宁愿先。

The result ( ruby 1.9.2p290 ):

结果(ruby 1.9.2p290):

Rehearsal -------------------------------------------------
regex           0.141000   0.000000   0.141000 (  0.140625)
regex2          0.125000   0.000000   0.125000 (  0.125000)
split[0]        0.031000   0.000000   0.031000 (  0.031250)
split[0],2      0.047000   0.000000   0.047000 (  0.046875)
split.first     0.031000   0.000000   0.031000 (  0.031250)
split.first,2   0.031000   0.000000   0.031000 (  0.031250)
---------------------------------------- total: 0.406000sec

                    user     system      total        real
regex           0.125000   0.000000   0.125000 (  0.125000)
regex2          0.109000   0.015000   0.124000 (  0.125000)
split[0]        0.031000   0.000000   0.031000 (  0.031250)
split[0],2      0.032000   0.000000   0.032000 (  0.031250)
split.first     0.047000   0.000000   0.047000 (  0.046875)
split.first,2   0.031000   0.000000   0.031000 (  0.031250)

The result ( ruby 1.8.7p352 ):

结果(ruby 1.8.7p352):

Rehearsal -------------------------------------------------
regex           0.060000   0.000000   0.060000 (  0.060661)
regex2          0.050000   0.000000   0.050000 (  0.049141)
split[0]        0.080000   0.000000   0.080000 (  0.083703)
split[0],2      0.070000   0.000000   0.070000 (  0.072780)
split.first     0.080000   0.000000   0.080000 (  0.080419)
split.first,2   0.070000   0.000000   0.070000 (  0.068370)
---------------------------------------- total: 0.410000sec

                    user     system      total        real
regex           0.060000   0.000000   0.060000 (  0.055427)
regex2          0.050000   0.000000   0.050000 (  0.048538)
split[0]        0.080000   0.000000   0.080000 (  0.084225)
split[0],2      0.070000   0.000000   0.070000 (  0.071673)
split.first     0.070000   0.000000   0.070000 (  0.079865)
split.first,2   0.060000   0.000000   0.060000 (  0.068685)

The Benchmark:

基准:

require 'benchmark'
TESTDATA = %w{
  37:48.1234567
  1:23:39.12357
  1:23:39.123.57
}
N = 10_000 #Number of Test loops

Benchmark.bmbm(10) {|b|

  b.report('regex') { N.times { TESTDATA.each{|str| str.gsub(/\.[^.]*\Z/, '') }} }
  b.report('regex2') { N.times { TESTDATA.each{|str| str.gsub(/\..*\Z/, '') }} }
  b.report('split[0]') { N.times { TESTDATA.each{| str | str.split(".")[0] }} }
  b.report('split[0],2') { N.times { TESTDATA.each{| str | str.split(".",2)[0] }} }
  b.report('split.first') { N.times { TESTDATA.each{| str | str.split(".").first }} }
  b.report('split.first,2') { N.times { TESTDATA.each{| str | str.split(".",2).first }} }
  #~ b.report('') { N.times { TESTDATA.each{| str | }} }
  #~ b.report('') { N.times { TESTDATA.each{|t| }} }
  #~ b.report('') { N.times { TESTDATA.each{|t| }} }

} #Benchmark

#3


3  

result = subject.gsub(/\.[^.]*\Z/, '')

does exactly this. It makes sure that if there is more than one decimal, only the last one will be affected.

正是如此。它确保如果有多个小数,则只会影响最后一个小数。

#1


13  

str.split(".")[0]

Hope that helps!

希望有所帮助!

#2


4  

I made a little benchmark with the solutions up to now. I modified the solutions a bit.

到目前为止,我对解决方案做了一些基准测试。我稍微修改了一下解决方案。

One unclear problem: Whats the result for 1:23:39.123.57? 1:23:39 or 1:23:39.123?

一个不明确的问题:结果是1:23:39.123.57? 1:23:39或1:23:39.123?

I choosed 1:23:39, only test regex has the other solution (see Tims answer)

我选择了1:23:39,只有测试正则表达式有另一种解决方案(见Tims回答)

The splitvariant has 2x2 variants: [0] or .first and split with a 2nd parameter , 2. The 2nd parameter of split limit the number of the splitted elements. We need only the first, so we may split in first and rest.

splitvariant具有2x2变体:[0]或.first并使用第二个参数2进行分割。分割的第二个参数限制分割元素的数量。我们只需要第一个,所以我们可以先分开休息。

The fastes solution: split('.', 2). If you use first or [0] makes no difference. I would prefer first.

紧固解决方案:拆分('。',2)。如果你先使用或[0]没有区别。我宁愿先。

The result ( ruby 1.9.2p290 ):

结果(ruby 1.9.2p290):

Rehearsal -------------------------------------------------
regex           0.141000   0.000000   0.141000 (  0.140625)
regex2          0.125000   0.000000   0.125000 (  0.125000)
split[0]        0.031000   0.000000   0.031000 (  0.031250)
split[0],2      0.047000   0.000000   0.047000 (  0.046875)
split.first     0.031000   0.000000   0.031000 (  0.031250)
split.first,2   0.031000   0.000000   0.031000 (  0.031250)
---------------------------------------- total: 0.406000sec

                    user     system      total        real
regex           0.125000   0.000000   0.125000 (  0.125000)
regex2          0.109000   0.015000   0.124000 (  0.125000)
split[0]        0.031000   0.000000   0.031000 (  0.031250)
split[0],2      0.032000   0.000000   0.032000 (  0.031250)
split.first     0.047000   0.000000   0.047000 (  0.046875)
split.first,2   0.031000   0.000000   0.031000 (  0.031250)

The result ( ruby 1.8.7p352 ):

结果(ruby 1.8.7p352):

Rehearsal -------------------------------------------------
regex           0.060000   0.000000   0.060000 (  0.060661)
regex2          0.050000   0.000000   0.050000 (  0.049141)
split[0]        0.080000   0.000000   0.080000 (  0.083703)
split[0],2      0.070000   0.000000   0.070000 (  0.072780)
split.first     0.080000   0.000000   0.080000 (  0.080419)
split.first,2   0.070000   0.000000   0.070000 (  0.068370)
---------------------------------------- total: 0.410000sec

                    user     system      total        real
regex           0.060000   0.000000   0.060000 (  0.055427)
regex2          0.050000   0.000000   0.050000 (  0.048538)
split[0]        0.080000   0.000000   0.080000 (  0.084225)
split[0],2      0.070000   0.000000   0.070000 (  0.071673)
split.first     0.070000   0.000000   0.070000 (  0.079865)
split.first,2   0.060000   0.000000   0.060000 (  0.068685)

The Benchmark:

基准:

require 'benchmark'
TESTDATA = %w{
  37:48.1234567
  1:23:39.12357
  1:23:39.123.57
}
N = 10_000 #Number of Test loops

Benchmark.bmbm(10) {|b|

  b.report('regex') { N.times { TESTDATA.each{|str| str.gsub(/\.[^.]*\Z/, '') }} }
  b.report('regex2') { N.times { TESTDATA.each{|str| str.gsub(/\..*\Z/, '') }} }
  b.report('split[0]') { N.times { TESTDATA.each{| str | str.split(".")[0] }} }
  b.report('split[0],2') { N.times { TESTDATA.each{| str | str.split(".",2)[0] }} }
  b.report('split.first') { N.times { TESTDATA.each{| str | str.split(".").first }} }
  b.report('split.first,2') { N.times { TESTDATA.each{| str | str.split(".",2).first }} }
  #~ b.report('') { N.times { TESTDATA.each{| str | }} }
  #~ b.report('') { N.times { TESTDATA.each{|t| }} }
  #~ b.report('') { N.times { TESTDATA.each{|t| }} }

} #Benchmark

#3


3  

result = subject.gsub(/\.[^.]*\Z/, '')

does exactly this. It makes sure that if there is more than one decimal, only the last one will be affected.

正是如此。它确保如果有多个小数,则只会影响最后一个小数。