Strings are everywhere. You deal with String instances not only every day, but probably every minute. They came from files, databases, REST APIs, or you simply use them to print results. It's a pervasive representation, and Ruby provides plenty to ease its manipulation. But String comes with its own share of problems and you won't always find a quick solution in the doc like how to deal with invalid byte sequence or convert back a String to a Date with an uncommon format.
Below, I share 7 common use cases of String I met very often and should be useful to you.
- How to remove enclosing characters like parenthesis?
- How to compare two Strings insensitively?
- How to clean a String by removing newline, carriage return, leading and trailing spaces?
- How to convert a String to a Regexp?
- How to convert a String to a Date with uncommon format?
- How to remove accents?
- How to clean up invalid byte sequences?
1. How to remove enclosing characters like parenthesis?
You retrieve data from a file:
The data is always wrapped by () that you don't want. You can get the data without them as following:
This actually removes the first and last characters of the String.
2. How to compare two Strings insensitively?
In an online shop, the user can search for product by their name:
You don't want to force your users to be case sensitive (they aren’t), so you can do:
As pointed out by apeiros in the comments, it won’t work for any non-ASCII characters. So, a better version would be:
3. How to clean a String by removing newline, carriage return, leading and trailing spaces?
In a little script, you read data from the console:
You can clean up what you read by simply doing:
Thanks to apeiros for making me notice that strip removes also trailing newline and carriage return. It's not needed to use chomp.
4. How to convert a String to a Regexp?
You're developing a Gem that maintains a database of locations of files. In the configuration file, the user can blacklist specific pathnames with regular expressions. You retrieved one of them (you don't want to locate the files in your trash):
You can then use it to directly check:
or keep it for later:
Don't forget to correctly escaping in your string, or even better, use Regexp.escape as suggested by apeiros.
5. How to convert a String to a Date with uncommon format?
In an old database, the dates are stored as String as following:
You can convert it as a Date as following:
6. How to remove accents?
You're building a web app and you'd like to allow a quick search among the members like
Accents and diacritics could be easily forgotten, and you want to remove them from the searched string. You can do it easily with ActiveSupport
7. How to clean up invalid byte sequences?
You read a text File encoded in UTF-8. You manipulate each of its line, but at some point you get the error
on the line
Well, you need to clean up your line before working on it by using scrub:
Old way: But if you have non-ASCII characters, it will remove them. In that case, you should go for a solution as apeiros' one.
What's your daily use cases of Strings?
What’s yours? What are your typical daily use cases of String and elegant Ruby to resolve them?