5

最後の要素を削除するにはどうすればよいですか? たとえば、次のような文字列があります。

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

出力を表示するきちんとした方法が必要です。

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

これは私ができることです:

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

これを1行で行うためのきちんとした方法があるかどうか疑問に思っていました: のように、代わりにstr.split(" ", 2).last => "testing string"表示する必要があります。"my testing"

編集

複数の興味深い回答をありがとうございました。あなたの努力と時間に感謝します。しかし、公平を期す必要があったので、全員の回答をベンチマークすることにしました。ベンチマーク付きのレポートは次のとおりです。

#!/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)

p str2[0...str2.rindex(' ')]これにより、答えがうまく機能していることが明らかになります。他のアプローチも好きでしたが、非常に思慮深く興味深いものでした。ありがとう。

第二版

@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)
4

3 に答える 3

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

そして、あなたが正規表現のファンでないなら:

str2 = "My another testing string"
p str2[0...str2.rindex(' ')] #=> My another testing
于 2013-10-03T10:36:17.720 に答える
10

次のようにsplit、最後の配列要素をスライスできます。

str1.split(' ')[0...-1].join(' ')
于 2013-10-03T10:32:34.387 に答える