ruby--string常用方法

一、字符串转换 

获取字符串转换方法 

"abc".methods.select { |e| e.to_s.end_with? "ize" }

常用转换方法

a = "abc def ghi jkl"

#所有字母大写
a.upcase! # => "ABC DEF GHI JKL"
#所有字母小写
a.downcase! # => "abc def ghi jkl"
#第一个单词首字母大写
a.capitalize! # => "Abc def ghi jkl"

p a.pluralize
#rails中的扩展
#将字第串首字母大写并将下划线转为空格
b = "this_is_a_string"

b.humanize! # => "This is a string"
#将字符串转为复数
b.pluralize # => "this_is_a_strings"
#将复数转为单数
b.pluralize.singularize # => "this_is_a_string"
#转为驼峰形式(字符串须以下划线为分隔符,不然效果和capitalize!一样)
b.camelize # => "ThisIsAString"
#转为以“-”连接的字符串
a.parameterize # => "abc-def-ghi-jkl" 
#所有单词首字母大写
a.titleize # => "Abc Def Ghi Jkl"

猜你喜欢

转载自www.cnblogs.com/ltns-hhyb/p/9074430.html