Ruby数组:更改字符串元素格式

时间:2023-02-15 21:44:28

I have an array of string which contains the "firstname.lastname" strings:

我有一个字符串数组,其中包含“firstname.lastname”字符串:

customers = ["aaa.bbb", "ccc.ddd", "www.uuu", "iii.oooo", ...]

Now, I would like to transfer each element's string format in the array by removing the "." and use space instead. That's I want the array to be:

现在,我想通过删除“。”来转移数组中每个元素的字符串格式。并使用空间代替。那是我想要的数组:

customers = ["aaa bbb", "ccc ddd", "www uuu", "iii oooo", ...]

What is the most efficient way to do it?

最有效的方法是什么?

---------------- MORE -------------------

- - - - - - - - 更多 - - - - - - - - - -

And how about my case here

那我的情况怎么样?

3 个解决方案

#1


7  

customers.collect!{|name| name.gsub(/\./, " ")}

Update

@tadman has it, here's my benchmarking FWIW

@tadman有它,这是我的基准测试FWIW

require 'benchmark'

customers = []
500000.times { customers << "john.doe" }

Benchmark.bm do|b|
  b.report("+= ") do
    # customers.collect!{|name| name.gsub(/\./, " ")} # 2.414220
    # customers.each { |c| c.gsub!(/\./, ' ') } # 2.223308
    customers.each { |c| c.tr!('.', ' ') } # 0.379226
  end
end

#2


2  

Don't know if this is the most efficient, but it does the job:

不知道这是否是最有效的,但它确实起作用:

customers.collect!{ |name| name.split('.').join(' ') }

#3


2  

You can always just modify each value in-place:

您始终可以就地修改每个值:

customers.each { |c| c.gsub!(/\./, ' ') }

The alternatives, like collect! are more appropriate when you're switching the kind of object, not just altering the content of an existing object.

替代品,如收集!当你切换对象的类型时,更合适,而不仅仅是改变现有对象的内容。

Note that this will mangle the original input, so if the String values in customers are frozen or need to be preserved in their initial form this won't work. This is probably not the case, but it is important to keep this in mind.

请注意,这将破坏原始输入,因此如果客户中的String值被冻结或需要以其初始形式保留,则这将不起作用。情况可能并非如此,但重要的是要记住这一点。

Update: After some benchmarking I've come to the conclusion that the fastest way to perform this operation is:

更新:经过一些基准测试后,我得出的结论是,执行此操作的最快方法是:

customers.each { |c| c.tr!('.', ' ') }

For those that are curious, here's a benchmark scaffold to experiment with.

对于那些好奇的人来说,这是一个可以尝试的基准脚手架。

  #     user     system      total        real
  # 0.740000   0.020000   0.760000 (  0.991042)
  list.each { |c| c.gsub!(/\./, ' ') }

  # 0.680000   0.010000   0.690000 (  1.011136)
  list.collect! { |c| c.gsub(/\./, ' ') }

  # 0.490000   0.020000   0.510000 (  0.628354)
  list.collect!{ |c| c.split('.').join(' ') }

  # 0.090000   0.000000   0.090000 (  0.103968)
  list.collect!{ |c| c.tr!('.', ' ') }

#1


7  

customers.collect!{|name| name.gsub(/\./, " ")}

Update

@tadman has it, here's my benchmarking FWIW

@tadman有它,这是我的基准测试FWIW

require 'benchmark'

customers = []
500000.times { customers << "john.doe" }

Benchmark.bm do|b|
  b.report("+= ") do
    # customers.collect!{|name| name.gsub(/\./, " ")} # 2.414220
    # customers.each { |c| c.gsub!(/\./, ' ') } # 2.223308
    customers.each { |c| c.tr!('.', ' ') } # 0.379226
  end
end

#2


2  

Don't know if this is the most efficient, but it does the job:

不知道这是否是最有效的,但它确实起作用:

customers.collect!{ |name| name.split('.').join(' ') }

#3


2  

You can always just modify each value in-place:

您始终可以就地修改每个值:

customers.each { |c| c.gsub!(/\./, ' ') }

The alternatives, like collect! are more appropriate when you're switching the kind of object, not just altering the content of an existing object.

替代品,如收集!当你切换对象的类型时,更合适,而不仅仅是改变现有对象的内容。

Note that this will mangle the original input, so if the String values in customers are frozen or need to be preserved in their initial form this won't work. This is probably not the case, but it is important to keep this in mind.

请注意,这将破坏原始输入,因此如果客户中的String值被冻结或需要以其初始形式保留,则这将不起作用。情况可能并非如此,但重要的是要记住这一点。

Update: After some benchmarking I've come to the conclusion that the fastest way to perform this operation is:

更新:经过一些基准测试后,我得出的结论是,执行此操作的最快方法是:

customers.each { |c| c.tr!('.', ' ') }

For those that are curious, here's a benchmark scaffold to experiment with.

对于那些好奇的人来说,这是一个可以尝试的基准脚手架。

  #     user     system      total        real
  # 0.740000   0.020000   0.760000 (  0.991042)
  list.each { |c| c.gsub!(/\./, ' ') }

  # 0.680000   0.010000   0.690000 (  1.011136)
  list.collect! { |c| c.gsub(/\./, ' ') }

  # 0.490000   0.020000   0.510000 (  0.628354)
  list.collect!{ |c| c.split('.').join(' ') }

  # 0.090000   0.000000   0.090000 (  0.103968)
  list.collect!{ |c| c.tr!('.', ' ') }