17 useful string handling methods in Ruby

Tram Ho

Every line of code you don’t write is one line of code you don’t have to maintain

String manipulation is something programmers do every day. In this post, I will discuss 17 efficient string methods provided by Ruby String Class, which will save you time in processing strings.

1. Repeat each character of a string

Often we need to loop through strings to process the characters. For example, you may want to print all the characters in the string

2. Converts a string to an array of characters

To convert a string into an array, we can use str.chars, which stands for str.each_char.to_a.

Now that we have an array, we can use any of the array’s methods. For example, the join method converts each element of the array to String, separated by the specified delimiter.

3. Get the length of a string

I think this is the most used method. Very useful when you want to check the string length before inserting it into the database table. Depending on your preferences, you can choose one of two.

4. Get the number of characters of a string

str.count takes one or more character sets as an argument. Then we take the intersection of these sets to get the final charset.

We can use it to count multiple characters, for example try counting lowercase letters

5. Reverse a string

String inversion can be useful, such as when you want to check if a string is a Palindrome prime.

6. Search for one or more characters of a string

str.include? returns true if there is any string or character, and false otherwise

7. Replace characters in a string

Replacing one or more characters of a string is a good way to clean up or format data. str.gsub replaces all occurrences with the provided strings. Here the first argument represents the character set we want to replace, à the second argument is the set replacement characters.

If you want to replace the first occurrence, use str.sub.

8. Split a string

Split a string based on a delimiter (default is a space) or a pattern.

You can limit the number of splits by providing a second argument

9. Cut a string

str.trim will remove any of the following first and last characters: null (” x00″), horizontal tab (” t”), line feed ( n), vertical tab (” v”), form feed (f), carriage return ( r), space (“”).

10. Cut the last character of a string

str.chomp removes a trailing character when provided with record delimiters or newline characters ( n, r and r n).

11. Add a string before another string

Appends one or more characters to a string at the beginning of the string.

12. Insert a string

Adds one or more characters to a specific position of a string.

13. Methods of changing the instance of a string

The str.downcase function converts each character of the string to lowercase.

str.upcase converts each character of a string to uppercase

The str.capitalize function converts the first character of the string to uppercase and the rest to lowercase

str.swapcase will convert the upper case to lower case and lower case to the upper case of a string.

14. Add string

One of the most frequent string operations is concatenation. To do that, we can use str.concat or <<.

15. Get the substring

The str.slice method is used when you want a specific part of a String, it returns a substring.

16. Find a string with the given prefix and suffix

We can test whether a string begins or ends with a string.

17. Check for empty strings

Perhaps the most frequently used method is str.empty, which can be used for data validation


Like other languages, Ruby has libraries of methods to help optimize performance. Using these 17 functions will save you a lot of time. Happy coding!



Share the news now

Source : Viblo