在 Ruby 中如何确定一个字符串是否以另一个字符串开头?

发布于 2024-10-02 08:08:23 字数 49 浏览 7 评论 0原文

在Ruby(没有rails)中查找一个字符串是否以另一个字符串开头的最佳方法是什么?

What the best way to find if a string starts with another in Ruby (without rails)?

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

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

发布评论

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

评论(4

把人绕傻吧 2024-10-09 08:08:23
puts 'abcdefg'.start_with?('abc')  #=> true

[编辑] 这是我在这个问题之前不知道的事情: start_with? 采用多个参数。

'abcdefg'.start_with?( 'xyz', 'opq', 'ab')
puts 'abcdefg'.start_with?('abc')  #=> true

[edit] This is something I didn't know before this question: start_with? takes multiple arguments.

'abcdefg'.start_with?( 'xyz', 'opq', 'ab')
浅暮の光 2024-10-09 08:08:23

由于这里介绍了多种方法,我想找出哪一种最快。使用 Ruby 1.9.3p362:

irb(main):001:0> require 'benchmark'
=> true
irb(main):002:0> Benchmark.realtime { 1.upto(10000000) { "foobar"[/\Afoo/] }}
=> 12.477248
irb(main):003:0> Benchmark.realtime { 1.upto(10000000) { "foobar" =~ /\Afoo/ }}
=> 9.593959
irb(main):004:0> Benchmark.realtime { 1.upto(10000000) { "foobar"["foo"] }}
=> 9.086909
irb(main):005:0> Benchmark.realtime { 1.upto(10000000) { "foobar".start_with?("foo") }}
=> 6.973697

所以看起来 start_with? 是其中最快的。

使用 Ruby 2.2.2p95 和更新的机器更新了结果:

require 'benchmark'
Benchmark.bm do |x|
  x.report('regex[]')    { 10000000.times { "foobar"[/\Afoo/] }}
  x.report('regex')      { 10000000.times { "foobar" =~ /\Afoo/ }}
  x.report('[]')         { 10000000.times { "foobar"["foo"] }}
  x.report('start_with') { 10000000.times { "foobar".start_with?("foo") }}
end

            user       system     total       real
regex[]     4.020000   0.000000   4.020000 (  4.024469)
regex       3.160000   0.000000   3.160000 (  3.159543)
[]          2.930000   0.000000   2.930000 (  2.931889)
start_with  2.010000   0.000000   2.010000 (  2.008162)

Since there are several methods presented here, I wanted to figure out which one was fastest. Using Ruby 1.9.3p362:

irb(main):001:0> require 'benchmark'
=> true
irb(main):002:0> Benchmark.realtime { 1.upto(10000000) { "foobar"[/\Afoo/] }}
=> 12.477248
irb(main):003:0> Benchmark.realtime { 1.upto(10000000) { "foobar" =~ /\Afoo/ }}
=> 9.593959
irb(main):004:0> Benchmark.realtime { 1.upto(10000000) { "foobar"["foo"] }}
=> 9.086909
irb(main):005:0> Benchmark.realtime { 1.upto(10000000) { "foobar".start_with?("foo") }}
=> 6.973697

So it looks like start_with? ist the fastest of the bunch.

Updated results with Ruby 2.2.2p95 and a newer machine:

require 'benchmark'
Benchmark.bm do |x|
  x.report('regex[]')    { 10000000.times { "foobar"[/\Afoo/] }}
  x.report('regex')      { 10000000.times { "foobar" =~ /\Afoo/ }}
  x.report('[]')         { 10000000.times { "foobar"["foo"] }}
  x.report('start_with') { 10000000.times { "foobar".start_with?("foo") }}
end

            user       system     total       real
regex[]     4.020000   0.000000   4.020000 (  4.024469)
regex       3.160000   0.000000   3.160000 (  3.159543)
[]          2.930000   0.000000   2.930000 (  2.931889)
start_with  2.010000   0.000000   2.010000 (  2.008162)
生活了然无味 2024-10-09 08:08:23

steenslag 提到的方法很简洁,考虑到问题的范围,它应该被认为是正确的答案。然而,还值得知道的是,这可以通过正则表达式来实现,如果您还不熟悉 Ruby,那么这是一项需要学习的重要技能。

尝试一下 Rubular: http://rubular.com/

但在这种情况下,以下 ruby​​ 语句将返回 true如果左侧的字符串以“abc”开头。右侧正则表达式中的 \A 表示“字符串的开头”。尝试一下 rubular——你就会清楚事情是如何运作的。

'abcdefg' =~  /\Aabc/ 

The method mentioned by steenslag is terse, and given the scope of the question it should be considered the correct answer. However it is also worth knowing that this can be achieved with a regular expression, which if you aren't already familiar with in Ruby, is an important skill to learn.

Have a play with Rubular: http://rubular.com/

But in this case, the following ruby statement will return true if the string on the left starts with 'abc'. The \A in the regex literal on the right means 'the beginning of the string'. Have a play with rubular - it will become clear how things work.

'abcdefg' =~  /\Aabc/ 
扶醉桌前 2024-10-09 08:08:23

我喜欢

if ('string'[/^str/]) ...

I like

if ('string'[/^str/]) ...
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文