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

时间:2021-11-06 13:05:40

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

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

7 个解决方案

#1


1386  

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

Ruby有一些方法可以改变字符串的情况。要转换为小写,请使用小写:

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

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

同样,upcase将每个字母大写,大写将字符串的第一个字母大写,其余字母小写:

"hello James!".upcase      #=> "HELLO JAMES!"
"hello James!".capitalize  #=> "Hello james!"
"hello James!".titleize    #=> "Hello James!"

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.

有关字符串的更多信息,请参阅文档。

#2


113  

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

您可以通过打开irb并运行以下命令找到字符串上所有可用的方法:

"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.

我用它来发现关于那些我可能不知道存在的物体的新的有趣的东西。

#3


36  

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

正如前面提到的@endeR一样,如果关注国际化,那么unicode_utils gem就足够了。

$ 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.

Ruby 2.4中的字符串操作现在是单处理器敏感的。

#4


14  

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

ruby downcase方法返回一个字符串,它的大写字母被小写字母替换。

"string".downcase

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

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

#5


11  

... and the uppercase is:

…大写字母是:

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

#6


11  

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

Rails Active Support gem为国际化支持提供了upcase、downcase、swapcase、大写等方法:

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

#7


0  

Since Ruby 2.4 there is a built in full Unicode case mapping. Source: https://*.com/a/38016153/888294. See Ruby 2.4.0 documentation for details: https://ruby-doc.org/core-2.4.0/String.html#method-i-downcase

自从Ruby 2.4以来,就有了完整的Unicode大小写映射。来源:https://*.com/a/38016153/888294。详细信息请参阅Ruby 2.4.0文档:https://ruby-doc.org/core-2.4.0/ str.html #method-i-downcase

#1


1386  

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

Ruby有一些方法可以改变字符串的情况。要转换为小写,请使用小写:

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

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

同样,upcase将每个字母大写,大写将字符串的第一个字母大写,其余字母小写:

"hello James!".upcase      #=> "HELLO JAMES!"
"hello James!".capitalize  #=> "Hello james!"
"hello James!".titleize    #=> "Hello James!"

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.

有关字符串的更多信息,请参阅文档。

#2


113  

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

您可以通过打开irb并运行以下命令找到字符串上所有可用的方法:

"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.

我用它来发现关于那些我可能不知道存在的物体的新的有趣的东西。

#3


36  

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

正如前面提到的@endeR一样,如果关注国际化,那么unicode_utils gem就足够了。

$ 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.

Ruby 2.4中的字符串操作现在是单处理器敏感的。

#4


14  

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

ruby downcase方法返回一个字符串,它的大写字母被小写字母替换。

"string".downcase

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

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

#5


11  

... and the uppercase is:

…大写字母是:

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

#6


11  

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

Rails Active Support gem为国际化支持提供了upcase、downcase、swapcase、大写等方法:

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

#7


0  

Since Ruby 2.4 there is a built in full Unicode case mapping. Source: https://*.com/a/38016153/888294. See Ruby 2.4.0 documentation for details: https://ruby-doc.org/core-2.4.0/String.html#method-i-downcase

自从Ruby 2.4以来,就有了完整的Unicode大小写映射。来源:https://*.com/a/38016153/888294。详细信息请参阅Ruby 2.4.0文档:https://ruby-doc.org/core-2.4.0/ str.html #method-i-downcase