![]()
|
|
Any developer worth his salt should be able to wrangle strings to his needs. |
|
Ruby provides so much string methods that you can walk around with a smirk like an old-western gunslinger who just put on his fully-loaded gun belt. |
|
Creating Strings |
|
There are many ways to create strings. |
|
You can use either single or double quotes to create strings. But use double quotes if you need to interpolate any ruby code: |
|
If you hate escaping special characters, use
|
|
You can also use something else with
|
|
But please, stick to one as much as you can. |
|
Use the weirdo ‘HEREDOCS’ if you want big, multi-line strings: |
|
Common String Methods |
|
Find length of string using |
|
You can change a string’s case using |
|
Reverse a string with |
|
The above methods don’t change the original string. They just return a new, modified string. But you can use the bang counterpart methods to change the original string itself. |
|
You can repeat a string ‘n’ times with the |
|
Checking the String for Something |
|
Is the string empty? |
|
Does it include this substring? |
|
Does it start with or end with this substring? |
|
Find the index of the substring: |
|
Find the index of the substring from the right-side: |
|
Now the opposite. Given an index or a range of indices, return the matching substring, aka ‘slicing’: |
|
Modifying the String |
|
Add 2 or more strings and return a new one: |
|
To add one or more strings to an existing string, use
the |
|
The shovel operator method |
|
To shovel multiple strings, do this: |
|
Replace the first occurrence of a string: |
|
Replace all occurrences: |
|
You can also use a regex pattern with |
|
Trim whitespaces with the |
|
Remove trailing newlines with |
|
You can insert a text into a string with |
|
You can also delete text within a string with |
|
Splitting and Joining a String |
|
Split based on a character, a substring or a pattern
with |
|
Join an array of strings using a joining character with
|
|
Mutable and Immutable Strings |
|
From the way we’re manhandling the strings above, you probably realized by now that in Ruby they’re mutable. |
|
But it’s nice to have immutable strings sometimes. Like
when you have to be sure that the value you defined
at the start of a program isn’t changed somewhere else
down the line. Use |
|
Maybe you could check first if it is |
|
But I lied. You can still ‘mutate’ it by simply re-assigning a new string to the same variable(?!): |
|
So a better way is to assign these strings to
Ruby constants. |
|
(Ruby constants are just variables beginning with a capital letter, and defined outside a method or a function, with an intention by the programmer to not have its value changed after it is first defined.) |
|
(Also, |
|
Strings vs Characters |
|
Strings are just group of characters. A single
character is still an object of the |
|
Use |
|
You can add longevity to your fingers by typing one character less when you define a character using the questionable quote: |
|
Encoding |
|
By default ruby strings are UTF-8 encoded. |
|
You can change the encoding of all strings in a file by
adding this comment as the first line in the file:
|
|
But if you only want to change a specific string’s
encoding, then use the |
|
Formatting String with Percent Literals |
|
If you want to do C-style “sprintf” string formatting,
you can do it with the elegant |
|
You can also use a hash for named substitutions: |
|
Lots of formatting options are available. See here. |
|
Regular Expression Support |
|
Ruby strings have great regex support with their
|
|
Official Docs
|
|
Links
|
Next topic: Symbols .