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

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.

主站蜘蛛池模板: 苍梧县| 阜城县| 玉溪市| 武鸣县| 望谟县| 渭南市| 南乐县| 象州县| 家居| 屯昌县| 繁昌县| 湖南省| 永和县| 尉犁县| 汾阳市| 萍乡市| 都昌县| 晴隆县| 深泽县| 聊城市| 张家川| 抚顺县| 新绛县| 鸡泽县| 房产| 任丘市| 沭阳县| 长垣县| 嵊泗县| 富裕县| 泊头市| 壤塘县| 石屏县| 林芝县| 乐至县| 永春县| 山东| 沙坪坝区| 伊宁县| 绥宁县| 邢台县|