删除 Ruby 中字符串的特定部分

发布于 2024-11-09 07:11:47 字数 186 浏览 0 评论 0原文

我有一个字符串 str = "abcdefghij",我想将 str2 设置为 str 减去第 4 个到第 6 个字符(假设基于 0 的索引)。

是否可以一次性完成此操作? slice! 似乎可以做到这一点,但它需要至少 3 个语句(复制、切片,然后使用字符串)。

I have a String str = "abcdefghij", and I want to set str2 to str minus the 4th to 6th character (assuming 0 based index).

Is it possible to do this in one go? slice! seems to do it, but it requires atleast 3 statements (duplicating, slicing, and then using the string).

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

七度光 2024-11-16 07:11:47

一种常见的方法是这样做:

str = "abcdefghij"
str2 = str.dup
str2[4..6] = ''
# => "abcdhij"

但仍然需要两个步骤。

如果你想要的范围是连续的,那么你可以一步完成

str2 = str[2..5]
# => "cdef"

A common way is to do it like this:

str = "abcdefghij"
str2 = str.dup
str2[4..6] = ''
# => "abcdhij"

but it still requires two steps.

If the range you want is continuous, then you can do it in one step

str2 = str[2..5]
# => "cdef"
七禾 2024-11-16 07:11:47

根据您要删除的具体内容, http://ruby-doc.org/ core/classes/String.html#M001201 可能是一个选择。

你可能可以用正则表达式做一些淫秽的事情:

"abcdefghij".sub(/(.{4}).{2}/) { $1 }

但这很恶心。

Depending on what exactly you're deleting, http://ruby-doc.org/core/classes/String.html#M001201 might be an option.

You could probably do obscene things with regexes:

"abcdefghij".sub(/(.{4}).{2}/) { $1 }

But that's gross.

心在旅行 2024-11-16 07:11:47

我继续使用以下解决方案:

str = "abcdefghij"
str2 = str[0, 4] + str[7..-1]

事实证明,它比提供的其他解决方案更快、更干净。这是一个迷你基准。

require 'benchmark'

str = "abcdefghij"
times = 1_000_000
Benchmark.bmbm do |bm|
  bm.report("1 step") do
    times.times do
      str2 = str[0, 4] + str[7..-1]
    end
  end
  bm.report("3 steps") do
    times.times do
      str2 = str.dup
      str2[4..6] = ''
      str2
    end
  end
end

Ruby 1.9.2 上的输出

Rehearsal -------------------------------------------
1 step    0.950000   0.010000   0.960000 (  0.955288)
3 steps   1.250000   0.000000   1.250000 (  1.250415)
---------------------------------- total: 2.210000sec

              user     system      total        real
1 step    0.960000   0.000000   0.960000 (  0.950541)
3 steps   1.250000   0.010000   1.260000 (  1.254416)

编辑:更新 <<

脚本:

require 'benchmark'

str = "abcdefghij"
times = 1_000_000
Benchmark.bmbm do |bm|
  bm.report("1 step") do
    times.times do
      str2 = str[0, 4] + str[7..-1] 
    end
  end
  bm.report("3 steps") do
    times.times do
      str2 = str.dup
      str2[4..6] = ''
      str2
    end
  end
  bm.report("1 step using <<") do
    times.times do
      str2 = str[0, 4] << str[7..-1] 
    end
  end
end

Ruby 1.9.2 上的输出

Rehearsal ---------------------------------------------------
1 step            0.980000   0.010000   0.990000 (  0.979944)
3 steps           1.270000   0.000000   1.270000 (  1.265495)
1 step using <<   0.910000   0.010000   0.920000 (  0.909705)
------------------------------------------ total: 3.180000sec

                      user     system      total        real
1 step            0.980000   0.000000   0.980000 (  0.985154)
3 steps           1.280000   0.000000   1.280000 (  1.281310)
1 step using <<   0.930000   0.000000   0.930000 (  0.916511)

I went ahead with using the following:

str = "abcdefghij"
str2 = str[0, 4] + str[7..-1]

It turned out to be faster and cleaner than the other solutions presented. Here's a mini benchmark.

require 'benchmark'

str = "abcdefghij"
times = 1_000_000
Benchmark.bmbm do |bm|
  bm.report("1 step") do
    times.times do
      str2 = str[0, 4] + str[7..-1]
    end
  end
  bm.report("3 steps") do
    times.times do
      str2 = str.dup
      str2[4..6] = ''
      str2
    end
  end
end

Output on Ruby 1.9.2

Rehearsal -------------------------------------------
1 step    0.950000   0.010000   0.960000 (  0.955288)
3 steps   1.250000   0.000000   1.250000 (  1.250415)
---------------------------------- total: 2.210000sec

              user     system      total        real
1 step    0.960000   0.000000   0.960000 (  0.950541)
3 steps   1.250000   0.010000   1.260000 (  1.254416)

Edit: Update for <<.

Script:

require 'benchmark'

str = "abcdefghij"
times = 1_000_000
Benchmark.bmbm do |bm|
  bm.report("1 step") do
    times.times do
      str2 = str[0, 4] + str[7..-1] 
    end
  end
  bm.report("3 steps") do
    times.times do
      str2 = str.dup
      str2[4..6] = ''
      str2
    end
  end
  bm.report("1 step using <<") do
    times.times do
      str2 = str[0, 4] << str[7..-1] 
    end
  end
end

Output on Ruby 1.9.2

Rehearsal ---------------------------------------------------
1 step            0.980000   0.010000   0.990000 (  0.979944)
3 steps           1.270000   0.000000   1.270000 (  1.265495)
1 step using <<   0.910000   0.010000   0.920000 (  0.909705)
------------------------------------------ total: 3.180000sec

                      user     system      total        real
1 step            0.980000   0.000000   0.980000 (  0.985154)
3 steps           1.280000   0.000000   1.280000 (  1.281310)
1 step using <<   0.930000   0.000000   0.930000 (  0.916511)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文