The .split Method on a String in Ruby

Boris Paskhaver
A free video tutorial from Boris Paskhaver
Software Engineer | Consultant | Author
4.7 instructor rating • 6 courses • 292,691 students

Lecture description

  • Call the .split method on a string to separate it by the occurrence of a delimiter and return an array of the results.

Learn more from the full course

Learn to Code with Ruby

A comprehensive introduction to coding with the Ruby programming language. Complete beginners welcome!

31:25:46 of on-demand video • Updated September 2020

  • Learn to program in the Ruby programming language
  • Master common Ruby objects like Strings, Arrays, Hashes, and more
  • Solve popular coding challenges and algorithms with Ruby
  • Dive into object-oriented programming including classes and more
English [Auto] Are right in this lesson we'll take a look at the split method and the split method is actually called on a String object. The reason it's shown up here is because the split method returns to us and array. So let's take a look here and go to define a variable called sentence and let's give it a nice. In fact it's going to be two senses but we can ignore the grammatical error for now. So. Hi my name is Boris. There are spaces here right like this let me buy some space right here. That looks good to me. All right. So what I'm going to do is just do a sentence that split for now I'm not going to give it any arguments. So what split does is it goes along the string and it splits it up based on a common delimiter. So whenever it runs into a certain character that's where it splits the string and whatever turns to us is an array of all of the values in the string just separated based on the comma delimiter. Now in this case if we don't provide an argument to the split method the default character or delimiter or that it's going to split everything by is going to be a space. So it's going to do is go through this string. It's going to run into these characters. This is the first sequence. It's then going to run into a space. So this is where it's going to separate or split the string. This first part is going to become the very first thing in our new array that is going to move on to my it's going to loop over that it's then going to run into another space so that's going to do another split there. My is going to become the second item in my array and so on is going to repeat that process all the way until the end of the string until it's all split up based on the common delimiter. So when I execute this method we're going to get back a brand new array. Here you can see on the right I have an array we can immediately tell by the square brackets on the left and right here. If I call the class method on it we can confirm that it is in fact an array. All right. So you can see here we've separated the string by spaces by default so none of the spaces here are actually present because each time it ran into a space it simply used that as a splitting point so high commas the first part followed by Maya followed by name followed by is followed by Borris and so on. Predictably the sput method also takes an argument and that argument allows us to specify another custom character by which we'd like to split. So as I mentioned the default option is a space or writing. This is equivalent to writing a split with the argument of a space. Here I am writing a string basically just passing and double quotes and what I put well within those two double quotes within my string represents the character that I want to split by or split on. So this what I have right here with this space is basically the default behavior. If I don't provide an argument it's going to split by a single occurrence of a space. But I can provide another argument here. For example if I wanted to split this by that occurrence of the period or the dot I can put a dot right here what it's going to do now is look for the first dot. It's going to find it right here at the end of Borris. So this first part is going to become the first item in Mirei and everything after the dot is going to become the second item within my array. So when execute this we're going to get a two item array. Predictably I'd split up and split up the string based on where that dot occurred. Similarly if I put in something like the letter m y the letter m appears right here so it's going to split here and appears here. So it's going to split here and there you see it's going to split several times in fact we can see that this is what it's going to give us after we split these these string based on common occurrences of the letter M and this doesn't have to be a single character. By the way if I want to do something like let's say space I don't know what it's going to do is split whenever it runs into space I you can see. Hi my name. And then the next element of the array it actually kicks off on the s because that eye that exists and is cut out because it's identified here as the delimiter. Right. So the reason this is beneficial is many times you can call the split method on a string to get back in array and then you can basically iterate over it and you know perform some kind of operations. So what I can do here for example is Do sentence start split and I don't even have to provide the argument of the space. Let's do it just just so we get used to syntax so here I'm going to provide a space. And this is going to give me of course an array of the words in the sentence because we split up each one each occurrence of space. I can store this in a variable called words. There is my words array right. Same thing. And what I can do now is iterate and do something with each word. So for example if I want to know the length of each word within my sentence string I can do words to each. Let's do curly braces here. Let's do a block variable and iterate over every word. So put that in my goal posts or my vertical pipes and what I want to do is output the words lengths. So now when I take a look at this we're going to get the length of every single word with in my sentence or the very first word here technically is high. But it also includes that comma at the end so high with the comma is three characters. My right here is going to be two characters name is going to be four. That's next in line and so on and so forth as it proceeds through everything. So the split method is really effective when it comes to splitting a string up. If we want to do things like iterate over its contents which many times we want to do especially if we're working with something like string text data from something like a file that we imported with Ruby. All right. So that's the split method on a string.