Ruby-on-rails 如何将字符串中每个单词的首字母大写
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19154369/
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
How to capitalize first letter of each word in the string
提问by kashif
How to capitalize first letter of each world in the string in Ruby on Rails:
如何在 Ruby on Rails 中将字符串中每个世界的第一个字母大写:
"goyette-xyz-is wide road".titleize returns "Goyette Xyz Is Wide Road".
I want the output like:
我想要这样的输出:
"goyette-xyz is wide road".SOME-FUNCTION should return "Goyette-xyz-is Wide Road".
titleize removes the underscore and hyphens but i want to keep it in the string.
titleize 删除下划线和连字符,但我想将其保留在字符串中。
回答by MZaragoza
you can just use the .titleizelike this "i want to make the first letter of each work into a cap".titleize
你可以.titleize像这样使用"i want to make the first letter of each work into a cap".titleize
you can learn more about titleizefrom the apidocks
您可以从 apidocks 中了解有关titleize 的更多信息
titleize(word) public
标题化(字)公开
Capitalizes all the words and replaces some characters in the string to create a nicer looking title. titleize is meant for creating pretty output. It is not used in the Rails internals.
将所有单词大写并替换字符串中的一些字符以创建更好看的标题。titleize 用于创建漂亮的输出。它不用于 Rails 内部。
titleize is also aliased as as titlecase.
titleize 也别名为 titlecase。
Examples:
例子:
"man from the boondocks".titleize # => "Man From The Boondocks"
"x-men: the last stand".titleize # => "X Men: The Last Stand"
"TheManWithoutAPast".titleize # => "The Man Without A Past"
"raiders_of_the_lost_ark".titleize # => "Raiders Of The Lost Ark"
how this actuality works
这种现实是如何运作的
# File activesupport/lib/active_support/inflector/methods.rb, line 115
def titleize(word)
humanize(underscore(word)).gsub(/\b('?[a-z])/) { .capitalize }
end
To Actually keep in "-" in the works we can add a new method to the string class like this.
为了在作品中实际保留“-”,我们可以像这样向字符串类添加一个新方法。
# ./lib/core_ext/string.rb
class String
#"goyette-xyz-is wide road".titleize_with_dashes#=> "Goyette-xyz-is Wide Road"
def titleize_with_dashes
humanize.gsub(/\b('?[a-z])/) { .capitalize }
end
end
回答by Marek Lipka
You could implement proper method by yourself:
你可以自己实现正确的方法:
class String
def my_titleize
split.map(&:capitalize).join(' ')
end
end
"goyette-xyz-is wide road".my_titleize
#=> "Goyette-xyz-is Wide Road"
回答by Jeremy F.
And if like for me right now you need to capitalize the first letter even for dashed words, you can do it like this:
如果现在像我一样,即使是虚线单词,您也需要将第一个字母大写,您可以这样做:
def titleize_and_keep_dashes(text)
text.split.map(&:capitalize).join(' ').split('-').map(&:titleize).join('-')
end
titleize_and_keep_dashes("goyette-xyz-is wide road")
# => "Goyette-Xyz Is Wide Road".
回答by ajknzhol
Add .capitalizemethod to your String to get the First letter capitalized automatically.
将.capitalize方法添加到您的字符串以自动将第一个字母大写。

