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

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.

主站蜘蛛池模板: 桑日县| 金塔县| 都兰县| 昌都县| 南城县| 聂荣县| 长海县| 安溪县| 尉犁县| 托克托县| 昌宁县| 三河市| 宁化县| 陇西县| 万全县| 宁都县| 常州市| 郸城县| 申扎县| 西畴县| 白玉县| 昌江| 体育| 延川县| 清涧县| 台江县| 靖西县| 九寨沟县| 东安县| 普兰县| 临澧县| 镇江市| 河间市| 大埔区| 县级市| 辰溪县| 明溪县| 榆社县| 广元市| 房山区| 双牌县|