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

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.

主站蜘蛛池模板: 阳谷县| 鄢陵县| 新沂市| 和田市| 黄大仙区| 肇州县| 阿克苏市| 班戈县| 德安县| 时尚| 临朐县| 德昌县| 莱芜市| 凤山市| 离岛区| 克拉玛依市| 屏边| 江永县| 淳化县| 涟水县| 固镇县| 东源县| 宁强县| 南和县| 漳平市| 嘉鱼县| 习水县| 巴青县| 嘉峪关市| 莱芜市| 高阳县| 忻州市| 赤峰市| 玉屏| 房产| 温泉县| 稻城县| 兴山县| 通山县| 天长市| 宁化县|