如何从句子中删除最后一个单词(一个字符串)

时间:2020-12-11 22:21:49

How to go about removing the last element? For example, I have strings like these:

如何删除最后一个元素?例如,我有这样的字符串:

str1 = "My testing String"
str2 = "My another testing string"

I need a neat way of showing the output:

我需要一种简洁的方式来显示输出:

str1 = "My testing"
str2 = "My another testing"

This is what I could do:

这就是我能做的:

str1 = str1.split(" ")
str1.delete(str1.last)
str1.join(" ")
# => "My testing"

I was wondering if there's any neat way of doing this maybe in one line, like: str.split(" ", 2).last => "testing string", which should show "my testing" instead.

我想知道是否有任何简洁的方法可以在一行中执行此操作,例如:str.split(“”,2).last =>“testing string”,它应显示“我的测试”。

EDIT

编辑

Thank you guys for the multiple and interesting answers. I appreciate your effort and time. But, I had to be fair, so I decided to benchmark everybody's answer. Here is the report with benchmark:

谢谢你们多个有趣的答案。我感谢你的努力和时间。但是,我必须公平,所以我决定对每个人的答案进行基准测试。以下是基准报告:

#!/usr/bin/ruby
require 'benchmark'
str2 = "My another testing string"
n = 500
Benchmark.bm(20) do |x|
  x.report("str2[/(.*)\s/,1]                  "){ n.times { str2[/(.*)\s/,1] } }
  x.report("str2[0...str2.rindex(' ')]        "){ n.times { str2[0...str2.rindex(' ')] } }
  x.report("str2.split(' ')[0...-1].join(' ') "){ n.times { str2.split(' ')[0...-1].join(' ') } }
  x.report("str2[/.*(?=\s)/]                  "){ n.times { str2[/.*(?=\s)/] } }
end


                                     user     system      total        real
str2[/(.*) /,1]                    0.000000   0.000000   0.000000 (  0.001394)
str2[0...str2.rindex(' ')]         0.000000   0.000000   0.000000 (  0.000956)
str2.split(' ')[0...-1].join(' ')  0.010000   0.000000   0.010000 (  0.002569)
str2[/.*(?= )/]                    0.000000   0.000000   0.000000 (  0.001351)

Which makes it clear that answer p str2[0...str2.rindex(' ')] is performing well. Though I liked other approaches as well, very thoughtful and interesting. Thanks.

这清楚地表明答案p str2 [0 ... str2.rindex('')]表现良好。虽然我也喜欢其他方法,但非常体贴和有趣。谢谢。

Second Edition

第二版

As per @theTineMan comment here is the updated benchmark:

根据@theTineMan的评论,这里是更新的基准:

require 'benchmark'
str2 = "My another testing string"
n = 999999
Benchmark.bmbm(20) do |x|
  x.report("str2[/(.*)\s/,1]                  "){ n.times { str2[/(.*)\s/,1] } }
  x.report("str2[0...str2.rindex(' ')]        "){ n.times { str2[0...str2.rindex(' ')] } }
  x.report("str2.split(' ')[0...-1].join(' ') "){ n.times { str2.split(' ')[0...-1].join(' ') } }
  x.report("str2[/.*(?=\s)/]                  "){ n.times { str2[/.*(?=\s)/] } }
end

Rehearsal ----------------------------------------------------------------------
str2[/(.*) /,1]                      1.030000   0.000000   1.030000 (  1.033787)
str2[0...str2.rindex(' ')]           0.850000   0.000000   0.850000 (  0.853124)
str2.split(' ')[0...-1].join(' ')    4.740000   0.000000   4.740000 (  4.750215)
str2[/.*(?= )/]                      0.990000   0.000000   0.990000 (  0.990726)
------------------------------------------------------------- total: 7.610000sec

                                         user     system      total        real
str2[/(.*) /,1]                      1.020000   0.000000   1.020000 (  1.014772)
str2[0...str2.rindex(' ')]           0.830000   0.000000   0.830000 (  0.839385)
str2.split(' ')[0...-1].join(' ')    4.620000   0.010000   4.630000 (  4.629874)
str2[/.*(?= )/]                      0.990000   0.000000   0.990000 (  0.988224)

3 个解决方案

#1


11  

str2 = "My another testing string"     
p str2[/(.*)\s/,1] #=> My another testing

And if you are not a fan on regexp:

如果你不是regexp的粉丝:

str2 = "My another testing string"
p str2[0...str2.rindex(' ')] #=> My another testing

#2


9  

With split you can slice the last array element like this:

使用split您可以像这样切片最后一个数组元素:

str1.split(' ')[0...-1].join(' ')

#3


7  

str1[/.*(?=\s)/]
# => "My testing"

str2[/.*(?=\s)/]
# => "My another testing"

#1


11  

str2 = "My another testing string"     
p str2[/(.*)\s/,1] #=> My another testing

And if you are not a fan on regexp:

如果你不是regexp的粉丝:

str2 = "My another testing string"
p str2[0...str2.rindex(' ')] #=> My another testing

#2


9  

With split you can slice the last array element like this:

使用split您可以像这样切片最后一个数组元素:

str1.split(' ')[0...-1].join(' ')

#3


7  

str1[/.*(?=\s)/]
# => "My testing"

str2[/.*(?=\s)/]
# => "My another testing"