Ruby capitalize every word first letter

前端 未结 8 805
名媛妹妹
名媛妹妹 2020-12-12 10:47

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

manufacturer.MFA_BRAND.first.upcase

is only sett

8条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2020-12-12 11:16

    I used this for a similar problem:

    'catherine mc-nulty joséphina'.capitalize.gsub(/(\s+\w)/) { |stuff| stuff.upcase }
    

    This handles the following weird cases I saw trying the previous answers:

    • non-word characters like -
    • accented characters common in names like é
    • capital characters in the middle of the string

提交回复
热议问题