Ruby on Rails 取消首字母大写

发布于 2024-10-08 07:46:21 字数 229 浏览 5 评论 0原文

我正在运行 Rails 2.3.2。

如何将 "Cool" 转换为 "cool"?我知道 "Cool".downcase 有效,但是是否有一种 Ruby/Rails 方法可以执行与 capitalize 相反的操作,即 uncapitalize 或 <代码>取消大写?

I'm running Rails 2.3.2.

How do I convert "Cool" to "cool"? I know "Cool".downcase works, but is there a Ruby/Rails method that does the opposite of capitalize, i.e., uncapitalize or decapitalize?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(11

寄与心 2024-10-15 07:46:22

没有大写的逆,但你可以随意推出自己的:

class String
  def uncapitalize 
    self[0, 1].downcase + self[1..-1]
  end
end

There is no inverse of capitalize, but you can feel free to roll your own:

class String
  def uncapitalize 
    self[0, 1].downcase + self[1..-1]
  end
end
清音悠歌 2024-10-15 07:46:22

您还可以使用简单的 sub 来完成此操作:

"Cool".sub(/^[A-Z]/) {|f| f.downcase }

You could also do this with a simple sub:

"Cool".sub(/^[A-Z]/) {|f| f.downcase }
倾城花音 2024-10-15 07:46:22
str = "Directly to the south"
str[0] = str[0].downcase
puts str
#=> "directly to the south"
str = "Directly to the south"
str[0] = str[0].downcase
puts str
#=> "directly to the south"
总以为 2024-10-15 07:46:22

没有真正的大写反转,但我认为 underscore 很接近。

"CoolCat".underscore  #=> "cool_cat"
"cool_cat".capitalize #=> "Cool_cat"
"cool_cat".camelize   #=> "CoolCat"

编辑: 下划线当然是camelize的反义词,而不是大写

There is no real inverse of capitalize, but I think underscore comes close.

"CoolCat".underscore  #=> "cool_cat"
"cool_cat".capitalize #=> "Cool_cat"
"cool_cat".camelize   #=> "CoolCat"

Edit: underscore is of course the inverse of camelize, not capitalize.

回忆躺在深渊里 2024-10-15 07:46:22

您可以使用 Tap (以便它适合一根线):

"JonSkeet".tap { |e| e[0] = e[0].downcase } # => "jonSkeet"

You can use tap (so that it fits on one line):

"JonSkeet".tap { |e| e[0] = e[0].downcase } # => "jonSkeet"
深爱不及久伴 2024-10-15 07:46:22

String#downcase_first (Rails 7.1+)

Rails 7.1 开始,有一个 String#downcase_first 方法:

将第一个字符转换为小写。

例如:

'If they enjoyed The Matrix'.downcase_first # => "if they enjoyed The Matrix"
'I'.downcase_first                          # => "i"
''.downcase_first                           # => ""

来源:

String#downcase_first (Rails 7.1+)

Starting from Rails 7.1, there is a String#downcase_first method:

Converts the first character to lowercase.

For example:

'If they enjoyed The Matrix'.downcase_first # => "if they enjoyed The Matrix"
'I'.downcase_first                          # => "i"
''.downcase_first                           # => ""

Sources:

夜唯美灬不弃 2024-10-15 07:46:22

有一个 capitalize 的逆过程,称为 swapcase

"Cool Cat".swapcase   #=> "cOOL cAT"

There is an inverse of capitalize called swapcase:

"Cool Cat".swapcase   #=> "cOOL cAT"
偏爱自由 2024-10-15 07:46:22

如果您使用 Ruby Facets,则可以小写第一个字母:

https://github.com/rubyworks/facets/blob/master/lib/core/facets/string/uppercase.rb

If you use Ruby Facets, you can lowercase the first letter:

https://github.com/rubyworks/facets/blob/master/lib/core/facets/string/uppercase.rb

吃不饱 2024-10-15 07:46:22
name = "Viru"

name = name.slice(0).downcase + name[1..(name.length)]
name = "Viru"

name = name.slice(0).downcase + name[1..(name.length)]
初吻给了烟 2024-10-15 07:46:22

试试这个

'Cool'.sub(/^([A-Z])/) { $1.tr!('[A-Z]', '[a-z]') }

https://apidock.com/ruby/XSD/CodeGen/GenSupport/uncapitalize< /a>

Try this

'Cool'.sub(/^([A-Z])/) { $1.tr!('[A-Z]', '[a-z]') }

https://apidock.com/ruby/XSD/CodeGen/GenSupport/uncapitalize

幼儿园老大 2024-10-15 07:46:21

还有:

"coolat_cat".camelize(:lower) # => "coolCat"

There is also:

"coolat_cat".camelize(:lower) # => "coolCat"
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文