DEV Community

Taylor D Jones
Taylor D Jones

Posted on

Ruby String Methods

Today I was working mainly with Ruby strings. It still surprises me how many of these methods cross over into Javascript, although they have different names. Let's explore some of the essentials Ruby string methods and what they do:

.reverse
This method returns a new string with the characters in reverse order. For example:
Ruby's .reverse method example

.upcase
returns a new string with all characters converted to uppercase. Example:
.upcase method example

.downcase
returns a new string with all characters converted to lowercase. Example:
.downcase method example

.swapcase
returns a new string with uppercase characters converted to lowercase and vice versa. Example:
I.swapcase method example

.capitalize
This method returns a new string with the first character converted to uppercase and the rest to lowercase. Example:
.capitalize method example

.strip
returns a new string with leading and trailing whitespace removed. For example:
.strip method example

.gsub
returns used for global substitution, replacing all occurrences of a pattern in a string with another string. For example:
.gsub method example

Until next time, happy coding!

Top comments (0)