官术网_书友最值得收藏!

String manipulation code examples

Let's start with an example. Let's say I want my application to always display the word Astros in capital letters. To do that, I simply write this:

"Astros".upcase 

Now if I always want a string to be in lower case letters, I can use the downcase method, like this:

"Astros".downcase 

These are the two methods I use quite often. However, there are other string methods available that we also have at our disposal.

For the rare times when you want to literally swap the case of the letters, you can leverage the swapcase method:

"Astros".swapcase 

Lastly, if you want to reverse the order of the letters in the string we can call the reverse method:

"Astros".reverse 

These methods are built into the String data class and we can call them on any string value in Ruby.

主站蜘蛛池模板: 铁岭市| 碌曲县| 锦州市| 西平县| 佳木斯市| 西乌珠穆沁旗| 全椒县| 靖宇县| 台东市| 新兴县| 富锦市| 柳江县| 宁南县| 安平县| 远安县| 张北县| 浮山县| 玉溪市| 招远市| 泗水县| 若尔盖县| 东光县| 垦利县| 民勤县| 道真| 进贤县| 荥经县| 江北区| 曲麻莱县| 清涧县| 大荔县| 潼关县| 灵川县| 铜山县| 临汾市| 上蔡县| 都昌县| 龙泉市| 射阳县| 石景山区| 罗城|