Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
537 views
in Technique[技术] by (71.8m points)

ruby - 如何在Ruby中将字符串转换为小写或大写(How to convert a string to lower or upper case in Ruby)

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

  ask by Heat Miser translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Ruby has a few methods for changing the case of strings.

(Ruby有几种更改字符串大小写的方法。)

To convert to lowercase, use downcase :

(要转换为小写,请使用downcase :)

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

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

(同样, upcase字母将每个字母capitalizecapitalize字母将第一个字母大写,其余字母小写:)

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

(有关更多信息,请参考String文档 。)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...