Ruby把每个单词的首字母大写

时间:2022-08-11 16:51:52

I need to make the first character of every word uppercase, and make the rest lowercase...

我需要把每个字的第一个字符大写,其余的小写……

manufacturer.MFA_BRAND.first.upcase

is only setting the first letter uppercase, but I need this:

只是把第一个字母大写,但我需要:

ALFA ROMEO => Alfa Romeo
AUDI => Audi
BMW => Bmw
ONETWO THREE FOUR => Onetwo Three Four

7 个解决方案

#1


191  

try this:

试试这个:

puts 'one TWO three foUR'.split.map(&:capitalize).join(' ')

#=> One Two Three Four

or

puts 'one TWO three foUR'.split.map(&:capitalize)*' '

#2


238  

In Rails:

在Rails中:

"kirk douglas".titleize => "Kirk Douglas"
#this also works for 'kirk_douglas'

w/o Rails:

w / o Rails:

"kirk douglas".split(/ |\_/).map(&:capitalize).join(" ")

#OBJECT IT OUT
def titleize(str)
  str.split(/ |\_/).map(&:capitalize).join(" ")
end

w/o Rails (load rails's ActiveSupport to patch #titleize method to String)

w/o Rails(将Rails的ActiveSupport加载到patch #titleize方法到String中)

require 'active_support/core_ext'
"kirk douglas".titleize #=> "Kirk Douglas"

(some) string use cases handled by #titleize

  • "kirk douglas"
  • “柯克·道格拉斯”
  • "kirk_douglas"
  • “kirk_douglas”
  • "kirk-douglas"
  • “柯克·道格拉斯”
  • "kirkDouglas"
  • “kirkDouglas”
  • "KirkDouglas"
  • “KirkDouglas”

#titleize gotchas

The #titleize method is a bit more complex than one might initially expect and can produce unexpected results, especially with case-sensitive situations as pointed out by @JamesMcMahon:

#titleize方法比人们最初预期的要复杂一些,并且可能产生意想不到的结果,特别是在区分大小写的情况下,如@JamesMcMahon所指出:

"hEy lOok".titleize #=> "H Ey Lo Ok"

because it is meant to handle camel-cased code like:

因为它是用来处理驼背式代码的:

"kirkDouglas".titleize #=> "Kirk Douglas"

To deal with this edge case you could clean your string with #downcase first before running #titleize. Of course if you do that you will wipe out any camelCased word separations:

要处理这种边缘情况,可以在运行#titleize之前先使用#downcase清理字符串。当然,如果你这样做了,你将会消灭任何驼背单词的分离:

"kirkDouglas".downcase.titleize #=> "Kirkdouglas"

#3


30  

"hello world".titleize which should output "Hello World".

“hello world”。titleize应该输出“Hello World”。

#4


18  

Another option is to use a regex and gsub, which takes a block:

另一种选择是使用regex和gsub,使用block:

'one TWO three foUR'.gsub(/\w+/, &:capitalize)

#5


3  

Look into the String#capitalize method.

查看字符串#大写方法。

http://www.ruby-doc.org/core-1.9.3/String.html#method-i-capitalize

http://www.ruby-doc.org/core-1.9.3/String.html method-i-capitalize

#6


3  

"hello world".split.each{|i| i.capitalize!}.join(' ')

#7


0  

If you are trying to capitalize the first letter of each word in an array you can simply put this:

如果你想把数组中每个单词的首字母大写,你可以简单地写上:

array_name.map(&:capitalize)

array_name.map(&:大写)

#1


191  

try this:

试试这个:

puts 'one TWO three foUR'.split.map(&:capitalize).join(' ')

#=> One Two Three Four

or

puts 'one TWO three foUR'.split.map(&:capitalize)*' '

#2


238  

In Rails:

在Rails中:

"kirk douglas".titleize => "Kirk Douglas"
#this also works for 'kirk_douglas'

w/o Rails:

w / o Rails:

"kirk douglas".split(/ |\_/).map(&:capitalize).join(" ")

#OBJECT IT OUT
def titleize(str)
  str.split(/ |\_/).map(&:capitalize).join(" ")
end

w/o Rails (load rails's ActiveSupport to patch #titleize method to String)

w/o Rails(将Rails的ActiveSupport加载到patch #titleize方法到String中)

require 'active_support/core_ext'
"kirk douglas".titleize #=> "Kirk Douglas"

(some) string use cases handled by #titleize

  • "kirk douglas"
  • “柯克·道格拉斯”
  • "kirk_douglas"
  • “kirk_douglas”
  • "kirk-douglas"
  • “柯克·道格拉斯”
  • "kirkDouglas"
  • “kirkDouglas”
  • "KirkDouglas"
  • “KirkDouglas”

#titleize gotchas

The #titleize method is a bit more complex than one might initially expect and can produce unexpected results, especially with case-sensitive situations as pointed out by @JamesMcMahon:

#titleize方法比人们最初预期的要复杂一些,并且可能产生意想不到的结果,特别是在区分大小写的情况下,如@JamesMcMahon所指出:

"hEy lOok".titleize #=> "H Ey Lo Ok"

because it is meant to handle camel-cased code like:

因为它是用来处理驼背式代码的:

"kirkDouglas".titleize #=> "Kirk Douglas"

To deal with this edge case you could clean your string with #downcase first before running #titleize. Of course if you do that you will wipe out any camelCased word separations:

要处理这种边缘情况,可以在运行#titleize之前先使用#downcase清理字符串。当然,如果你这样做了,你将会消灭任何驼背单词的分离:

"kirkDouglas".downcase.titleize #=> "Kirkdouglas"

#3


30  

"hello world".titleize which should output "Hello World".

“hello world”。titleize应该输出“Hello World”。

#4


18  

Another option is to use a regex and gsub, which takes a block:

另一种选择是使用regex和gsub,使用block:

'one TWO three foUR'.gsub(/\w+/, &:capitalize)

#5


3  

Look into the String#capitalize method.

查看字符串#大写方法。

http://www.ruby-doc.org/core-1.9.3/String.html#method-i-capitalize

http://www.ruby-doc.org/core-1.9.3/String.html method-i-capitalize

#6


3  

"hello world".split.each{|i| i.capitalize!}.join(' ')

#7


0  

If you are trying to capitalize the first letter of each word in an array you can simply put this:

如果你想把数组中每个单词的首字母大写,你可以简单地写上:

array_name.map(&:capitalize)

array_name.map(&:大写)