如何在 Ruby 中将字符串转换为小写或大写

发布于 2024-07-25 11:35:29 字数 34 浏览 7 评论 0原文

在 Ruby 中如何获取字符串并将其转换为小写或大写?

How do I take a string and convert it to lower or upper case in Ruby?

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

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

发布评论

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

评论(11

永不分离 2024-08-01 11:35:29

Ruby 有几种更改字符串大小写的方法。 要转换为小写,请使用 downcase

"hello James!".downcase    #=> "hello james!"

类似地,upcase 将每个字母大写,而 capitalize 将字符串的第一个字母大写,但将其余字母小写:

"hello James!".upcase      #=> "HELLO JAMES!"
"hello James!".capitalize  #=> "Hello james!"
"hello James!".titleize    #=> "Hello James!" (Rails/ActiveSupport only)

如果如果要就地修改字符串,可以向其中任何方法添加感叹号:

string = "hello James!"
string.downcase!
string   #=> "hello james!"

请参阅 字符串文档了解更多信息。

Ruby has a few methods for changing the case of strings. To convert to lowercase, use downcase:

"hello James!".downcase    #=> "hello james!"

Similarly, upcase capitalizes every letter and capitalize capitalizes the first letter of the string but lowercases the rest:

"hello James!".upcase      #=> "HELLO JAMES!"
"hello James!".capitalize  #=> "Hello james!"
"hello James!".titleize    #=> "Hello James!" (Rails/ActiveSupport only)

If you want to modify a string in place, you can add an exclamation point to any of those methods:

string = "hello James!"
string.downcase!
string   #=> "hello james!"

Refer to the documentation for String for more information.

半岛未凉 2024-08-01 11:35:29

您可以通过打开 irb 并运行来找出字符串上可用的所有方法:

"MyString".methods.sort

特别是可用于字符串的方法列表:

"MyString".own_methods.sort

我用它来查找有关对象的新的和有趣的事情,否则我可能不知道这些对象的存在。

You can find out all the methods available on a String by opening irb and running:

"MyString".methods.sort

And for a list of the methods available for strings in particular:

"MyString".own_methods.sort

I use this to find out new and interesting things about objects which I might not otherwise have known existed.

浅浅 2024-08-01 11:35:29

就像 @endeR 提到的,如果国际化是一个问题,那么 unicode_utils gem 就足够了。

$ gem install unicode_utils
$ irb
> require 'unicode_utils'
=> true
> UnicodeUtils.downcase("FEN BİLİMLERİ", :tr)
=> "fen bilimleri"

Ruby 2.4 中的字符串操作现在对 unicode 敏感。

Like @endeR mentioned, if internationalization is a concern, the unicode_utils gem is more than adequate.

$ gem install unicode_utils
$ irb
> require 'unicode_utils'
=> true
> UnicodeUtils.downcase("FEN BİLİMLERİ", :tr)
=> "fen bilimleri"

String manipulations in Ruby 2.4 are now unicode-sensitive.

留蓝 2024-08-01 11:35:29

ruby downcase 方法返回一个字符串,其中大写字母替换为小写字母。

"string".downcase

https://ruby-doc.org/core-2.1 .0/String.html#method-i-downcase

The ruby downcase method returns a string with its uppercase letters replaced by lowercase letters.

"string".downcase

https://ruby-doc.org/core-2.1.0/String.html#method-i-downcase

笑,眼淚并存 2024-08-01 11:35:29

...大写的是:

"Awesome String".upcase
=> "AWESOME STRING"

... and the uppercase is:

"Awesome String".upcase
=> "AWESOME STRING"
勿忘心安 2024-08-01 11:35:29

Rails Active Support gem 提供了 upcasedowncaseswapcasecapitalize 等具有国际化支持的方法:

gem install activesupport
irb -ractive_support/core_ext/string
"STRING  ÁÂÃÀÇÉÊÍÓÔÕÚ".mb_chars.downcase.to_s
 => "string  áâãàçéêíóôõú"
"string  áâãàçéêíóôõú".mb_chars.upcase.to_s
=> "STRING  ÁÂÃÀÇÉÊÍÓÔÕÚ"

The Rails Active Support gem provides upcase, downcase, swapcase,capitalize, etc. methods with internationalization support:

gem install activesupport
irb -ractive_support/core_ext/string
"STRING  ÁÂÃÀÇÉÊÍÓÔÕÚ".mb_chars.downcase.to_s
 => "string  áâãàçéêíóôõú"
"string  áâãàçéêíóôõú".mb_chars.upcase.to_s
=> "STRING  ÁÂÃÀÇÉÊÍÓÔÕÚ"
回忆追雨的时光 2024-08-01 11:35:29

.swapcase 方法将字符串中的大写字母转换为小写,将小写字母转换为大写。

'TESTING'.swapcase #=> testing
'testing'.swapcase #=> TESTING

The .swapcase method transforms the uppercase letters in a string to lowercase and the lowercase letters to uppercase.

'TESTING'.swapcase #=> testing
'testing'.swapcase #=> TESTING
嘿看小鸭子会跑 2024-08-01 11:35:29

您可以找到像 "strings".methods 这样的字符串方法
您可以将字符串定义为upcasedowncasetitleize
例如,

"hii".downcase
"hii".titleize
"hii".upcase

You can find strings method like "strings".methods
You can define string as upcase, downcase, titleize.
For Example,

"hii".downcase
"hii".titleize
"hii".upcase
小糖芽 2024-08-01 11:35:29

并不适用于所有人,但这节省了我很多时间。 我刚刚遇到了 CSV 返回“TRUE 或“FALSE”的问题,所以我刚刚添加了 VALUE.to_s.downcase ==“true”,如果值为“TRUE”,则返回布尔值 true;如果值为“FALSE”,则返回 false ",但仍然适用于布尔值 true 和 false。

Won't work for every, but this just saved me a bunch of time. I just had the problem with a CSV returning "TRUE or "FALSE" so I just added VALUE.to_s.downcase == "true" which will return the boolean true if the value is "TRUE" and false if the value is "FALSE", but will still work for the boolean true and false.

-黛色若梦 2024-08-01 11:35:29

try 方法结合,支持 nil 值:

'string'.try(:upcase)
'string'.try(:capitalize)
'string'.try(:titleize)

In combination with try method, to support nil value:

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