Split and Strip Guides for Strings in Ruby
In this lesson, we are going to see the important **split** and **strip** methods in Ruby strings, these methods will help us clean up strings and convert a string to an array so we can access each word as its own value.
Guide Tasks
  • Read Tutorial
  • Watch Guide Video
Video locked
This video is viewable to users with a Bottega Bootcamp license

In this lesson, we are going to walk through how to use the split and strip methods in Ruby. These methods will help us clean up strings and convert a string to an array so we can access each word as its own value.

Using the strip method

Let's start off by analyzing the strip method. Imagine that the input you get from the user or from the database is poorly formatted and contains white space before and after the value. To clean the data up we can use the strip method. For example:

str = "     The quick brown fox jumped over the quick dog       "
p str.strip

When you run this code, the output is just the sentence without the white space before and after the words.

large

Using the split method

Now let's walk through the split method. Split is a powerful tool that allows you to split a sentence into an array of words or characters. For example, when you type

str = "The quick brown fox jumped over the quick dog"
p str.split

You'll see that it converts the sentence into an array of words.

large

This method can be particularly useful for long paragraphs, especially when you want to know the number of words in the paragraph. Since the split method converts the string into an array, you can use all the array methods like size to see how many words were in the string.

We can leverage method chaining to find out how many words are in the string, like so:

str = "The quick brown fox jumped over the quick dog"
p str.split.size

it should return a value of 9, which is the number of words in the sentence.

large

To know the number of letters, we can pass an optional argument to the split method and use the format:

str = "The quick brown fox jumped over the quick dog"
p str.split(//).size
```

![large](https://s3.amazonaws.com/devcamp-publishing/comprehensive-ruby/003-strings/05-split-and-strip-guides-for-strings-in-ruby/Snip20160901_27.png)

And if you want to see all of the individual letters, we can remove the `size` method call, like this:

```ruby
p str.split(//)
```

And your output should look like this:

![large](https://s3.amazonaws.com/devcamp-publishing/comprehensive-ruby/003-strings/05-split-and-strip-guides-for-strings-in-ruby/Snip20160901_28.png)

Notice, that it also included spaces as individual characters. Which may or may not be what you want a program to return.

This method can be quite handy while developing real world applications. A good practical example of this method is Twitter.  Since this social media site restricts users to 140 characters, this method is sure to be a part of the validation code that counts the number of characters in a Tweet.