创建一个 titleize 方法,排除“小词". [英] Create a titleize method, that excludes "little words".
本文介绍了创建一个 titleize 方法,排除“小词".的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在制作一种方法来将我的输入大写,除了 a、an 和...等任何单词.
I'm making a method to capitalize my input, except any words such as a, an, and...etc.
def titleize(string_to_titleize)
string_to_titleize.split(' ').map { |words| words.capitalize }.join(' ')
end
我知道有一些宝石可以做到这一点.我无法掌握如何手动完成.我假设创建一个不大写的单词列表.然后,将他们排除在某人之外.
I understand there are gems to do this. I can't grasp how to do it manually. I assume to create a list of words not to be capitalized. Then, exlude them someone.
推荐答案
arr = ['a', 'an', 'the']
str ="This is a salil gaikwad working as an engineer"
str.gsub(/\w+/) {|match| arr.include?(match) ? match : match.capitalize}
#Gives o/p :- This Is a Salil Gaikwad Working As an Engineer
这篇关于创建一个 titleize 方法,排除“小词".的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文