Ruby 将每个单词的第一个字母大写

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13520162/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-06 05:32:04  来源:igfitidea点击:

Ruby capitalize every word first letter

rubystring

提问by byCoder

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

回答by boulder_ruby

In Rails:

在导轨中:

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

w/o Rails:

不带导轨:

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

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

#OR MONKEY PATCH IT
class String  
  def titleize
    self.split(/ |\_/).map(&:capitalize).join(" ")
  end
end

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

w/o Rails(加载 rails 的 ActiveSupport 以将 #titleize 方法修补到String

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

(some) string use cases handled by #titleize

#titleize 处理的(一些)字符串用例

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

#titleize gotchas

#titleize 陷阱

Rails's titleizewill convert things like dashes and underscores into spaces and can produce other unexpected results, especially with case-sensitive situations as pointed out by @JamesMcMahon:

Railstitleize会将破折号和下划线之类的内容转换为空格,并可能产生其他意想不到的结果,尤其是@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 #downcasefirst before running #titleize. Of course if you do that you will wipe out any camelCased word separations:

要处理这种#downcase极端情况,您可以在运行#titleize 之前先使用清理字符串。当然,如果你这样做,你将消除任何驼峰式分词:

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

回答by boulder_ruby

try this:

尝试这个:

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

#=> One Two Three Four

or

或者

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

回答by tint lwin lwin win

"hello world".titleizewhich should output "Hello World".

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

回答by Bob Nadler

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

另一种选择是使用正则表达式和 gsub,它需要一个块:

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

回答by Robert 'Jet' Rowe

回答by Muhamamd Awais

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

回答by astee

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(&:大写)