Working with Strings
Learn about the variables of String type and how you can operate with them in Ruby.
We'll cover the following
A String
object represents text
A String
object, or simply string, is an object that represents a specific text. For example, your name is represented as one string in Ruby. The title of this text is another string, and so is this paragraph, as is this full text.
Strings are everywhere in web applications. In fact, they’re used far more often than numbers. That’s probably because most popular applications deal with descriptive text (Email, Facebook, Twitter, Amazon, eBay, and so on).
Another example are those complicated web-based calculator applications that deal with numbers.
In Ruby, there are a couple of flexible ways to create strings, but the most simple and common way is to enclose some characters in quotes:
"This is one string!"
'And this is another one.'
Remember: Strings can be defined by enclosing any text with single or double quotes.
Both of these are good to use. Technically, they are almost identical, except for one important feature called string interpolation, which we’ll explain later.
Things we can do with strings
Here are a few things we can do with strings.
We can glue them together by using +
. We call this concatenation.
puts "snow" + "ball"
puts "hi" + "hi" + "hi"
The last operation can also be done by using *
, like so:
puts "hi" * 3
Multiplying a string by a number in Ruby means repeating the string that many times.
The following examples are worth noting too:
puts "1" + "1" + "1"puts "1" * 3
This demonstrates that Ruby behaves the same for strings that contain nothing but numbers.
puts "hello".upcaseputs "hello".capitalizeputs "hello".lengthputs "hello".reverseputs "hello world".split("l")
These last few examples illustrate the calling of methods on string objects. Methods are behaviors that objects are capable of performing. We’ll explore methods in more detail later.
History
Pieces of text, in the context of programming languages, aren’t usually referred to as text, but instead with the slightly odd term “string.” There is a technical reason for this.
In early programming languages, strings were implemented as lists of characters, and programmers had to deal with them as such. Imagine pellets lined up on a string. We had to manually manage the length of the character list, and inserting a couple of characters in the middle of such a list was quite a complicated operation.
As languages evolved, they made things like this easier for developers and added built-in abstractions for the concept of strings. So, the name stuck and resembles the way programmers had thought about the text before: characters lined up on strings.
Luckily, nowadays, in Ruby and any other modern language, we can think of strings simply as text.
Further readings
Look through the documentationfor the String
class.
- Notice any interesting methods?
- Find out how to convert the string “1.23” into the number 1.23. If you can’t find it, do an internet search for “ruby convert string to float.”