Locked lesson.
About this lesson
Modifying a string is simple with these string manipulation tips.
Exercise files
Download this lesson’s related exercise files.
String Manipulation - Solution.docx59 KB String Manipulation.docx
60.1 KB
Quick reference
String Manipulation
There are many ways to manipulate strings with Ruby.
When to use
Use these whenever you want to manipulate strings.
Instructions
Here are some built-in Ruby methods that let you manipulate strings:
.downcase - changes all letters to lowercase
.upcase - changes all letters to uppercase
.capitalize - capitalizes first letter
.reverse - reverses letters in a word
.swapcase - reverses the case of your words
.length - counts the number of characters in a string
Hints & tips
- Use a built-in Ruby method to manipulate strings!
- 00:04 In this video I want to talk about string manipulation.
- 00:07 And we've used strings a whole bunch throughout this course, it's just,
- 00:12 puts Hello World, this is a string,
- 00:14 all the stuff inside of the quotation marks is a string.
- 00:17 Now strictly speaking, a string, is a ruby class, right?
- 00:21 And inside that class,
- 00:22 there's lots of methods that you can use to do stringy type things.
- 00:27 And you can manipulate strings with those methods, and
- 00:29 we're going to look at some of those methods in this video.
- 00:32 We haven't really talked about methods, and classes, and things like that.
- 00:35 So at this point it might be a little bit over your head, but
- 00:37 don't worry about it it's basically just something we can do to our string, right?
- 00:41 So let's create a little program, let's go print, enter your name, and then say,
- 00:48 name = gets, let's chomp this thing, you're always going to want to chomp your gets.
- 00:53 And then let's say puts "Hello",
- 00:57 let's do our interpolation, hashtag and our curly braces and name.
- 01:02 I like to put spaces, you don't have to,
- 01:04 you could do it like this, but I find it's easier to read if you do it like that.
- 01:08 So we save this.
- 01:10 If we run this program, Enter your name:, John, Hello, John.
- 01:14 So we've seen this before, no big deal.
- 01:16 Now let's say we want to mess around with our string, we want to manipulate it.
- 01:19 Well, we can do all kinds of cool stuff,
- 01:21 let's say we want the output to be in lowercase.
- 01:24 I could just type in .downcase, save this, and
- 01:28 now if we run it again, enter your name: John.
- 01:31 Notice that my name is uppercase J-o-h-n, but when it outputs it,
- 01:35 it outputs it as lowercase.
- 01:37 Ruby has converted it to downcase,
- 01:39 by just slapping this .downcase on there, so that's very cool.
- 01:43 We can do upcase, if we save this and run it, if I type in john, lowercase,
- 01:49 it uppercases every single letter in my string, so that's kind of interesting.
- 01:54 So downcase, upcase, we can capitalize, if we save this,
- 01:58 what do you think this is going to do?
- 02:00 If I type in lowercase john, it capitalizes it for
- 02:03 me, it just does the first letter of the word.
- 02:05 We can do reverse.
- 02:07 This is a fun one.
- 02:09 If I type in John, it outputs nhoJ, right?
- 02:14 So if we run it again, and I type in racecar, it outputs racecar,
- 02:19 because racecar spelled backwards is racecar.
- 02:22 We can swapcase, if we run this,
- 02:27 John, I misspelled swap.
- 02:30 See, always getting errors.
- 02:32 It swapped the case.
- 02:35 Now I typed in capital J and lowercase o-h-n, it outputs lowercase j and
- 02:39 capital O-H-N, so that's kind of fun.
- 02:42 We have, this is a very useful one, length,
- 02:45 those first few were just kind of fun, this is actually very, very useful.
- 02:49 So if we save it and run it,
- 02:50 If I type in John, what do you think it's going to return?
- 02:53 4.
- 02:55 So it says, how many characters are in this string?
- 02:58 One, two, three, four.
- 03:01 So that is very useful.
- 03:03 So it doesn't matter how many, it'll tell me 48, very useful.
- 03:07 We're going to see lots of reasons to use that later on, especially with arrays and
- 03:10 things like that, loops, different things we're going to use, at.length method for.
- 03:15 So that's string manipulation.
- 03:16 Just very easy to do all kinds of weird things, and there's lots of these little
- 03:20 methods, you can look them up, string manipulation methods.
- 03:23 You could look up the string class.
- 03:25 I'll show you how to do stuff like that later on in the course, to learn about
- 03:28 these different fun little things you can do with strings, and other things as well.
- 03:31 But, that's pretty much it, we are done now with the basics of Ruby.
- 03:34 In the next section, we're going to move into intermediate Ruby and
- 03:37 we're going to look at arrays.
Lesson notes are only available for subscribers.