Learn String Functions in C++

You can access the full course here: The Complete Introduction to C++

Now that we know how to use functions, let’s take a look at some library functions. Strings come with a lot of functionality that can help retrieve properties or modify the string in some way. We will take a look at 6 functions:

  • size()
  • empty()
  • substr(index, length)
  • append(string)
  • insert(index, string)
  • erase(index, length)
std::string name = “Nimish”;
name.size(); // 6
name.empty(); // false
name.substr(0, 3); // “Nim”

Indexing starts at 0 instead of 1. Now for some modification functions. Let’s say we wanted to change the string “Nimesh” into “Nimish Narang” we could do this:

name = “Nimesh”;
name.insert(3, “i”); // name = “Nimiesh”
name.erase(4, 1); // name = “Nimish”
name.append(“ Narang”); // name = “Nimish Narang”

Transcript

What is up everyone? Welcome to our tutorial on string functions. Seeing us we’re quite familiar with functions and strings, I thought we’d end this with a little bit of fun. Getting to know some of the really cool things that we can do with strings. So, what are string functions?

Well, strings have many functions attached to them that either help retrieve properties or modify the string in some way, and this has because they are advanced data types, actually a bit more similar to erase, than something that we’ve seen until this point.

So, these six functions that we’re gonna look at, will be size, empty, substring, append, insert and erase, although that is just six of many possible functions. There is a link down there below that contains a reference to all of the possible string functions available.

So, let’s head over to the code, we’ll check out these six functions and then talk about what more we can do. Okay, so I figured this is really just kind of a for fun section.

If you’re really not that interested in string functions, you can skip this and end up the course, but I figured it’s always good to get to know some of the cool things that we can do with strings, because they’re such a versatile data type.

So, I’m gonna start off with a string that represents my name, and I’m just gonna play around with it a little bit. So, like I said, we’ll explore those six functions. The first of which is the, size.

So, it’s always good to know how long your string is, because strings have no predetermined length, we don’t necessarily know how long they are. So for that, we can simply call upon the string name and then dot and then the size.

So, this is the length or size property of a string, in our case, we might want to print this out. So, I’m just going to wrap this in a print statement, like so, okay. And then we’re just gonna end the line here. Okay, so this should return a six, indeed it does.

There are six characters in the string, it’s off length six, and so that’s where that comes from. The next one is to determine whether or not the string is empty. So, if the string is empty, that means it looks like this.

In our case, we know that’s not true because we have six characters here, but nevertheless, it’s always a good one to know. So, this just returns true or false based on whether or not the string is empty. In this case, we get false because it is not empty.

So, those are the two more commonly used property retrieval functions. I guess you could say that this third function is sort of retrieving a property. Although more specifically, it’s actually retrieving a part of the string, and that is our, name . substring function.

So, it looks like this, it’s a short for substring. So the purpose of this function is just to retrieve a part of a string, hence a substring, okay? If we open up the brackets, we get the arguments and exactly what they are.

The first is going to be the position that we want to start at, and then the second is going to be the length or the size of the sub-string. So, let’s say I want my first three characters, it’s just going to be name.

The start position is zero, because this is position zero, one, two, three, four and five, and my length is going to be three. So, I can, again, print that out. Why don’t we do that? Just going to take this, cut it, get rid of that extra semi-colon and then we’ll paste it in here.

And this will just return me, my first three characters, okay? So, those are the three retrieval functions I wanted to go over. Now, we’re gonna turn to three modification functions. These are going to be our append, insert and erase functions.

The append function is probably the easiest of them, and as far as strings are concerned acts very similar to the plus operator. It essentially takes one string and sticks it onto the end of the other.

So, let’s say for example, I wanted my first name to be my full name, okay? So, what I could do is I could take this name, call the append function, and then I want to stick this string, but I’m gonna give a space as well onto the end of it.

And then if I print out my name, I should get my full name printed out. So, we’re going to replace that with just my name. Now, keep in mind, this function does actually modify the original string value and that’s where it’s actually different from the plus operator.

The plus operator will return a new string, this modifies the original. So, if we run this, we should see the full name printed out here, and that’s exactly what we get. Again, it modifies the string whereas the plus operator produces a brand new string.

Okay, so we’re going to resort back to this being our name or whatever your name is, feel free to sub it in, but we’re gonna make a couple of spelling mistakes, at least I will in my own name.

So, I’m actually gonna set name equal to something else. I’m gonna replace the I with an E, just so I can show you a couple ways we can fix it. Now, there is actually a replace operation, but we’ll be doing two things instead.

We’ll be doing the insert and then the erase, rather than the replace, because I figure it’s better to get to know two functions for the price of one. So, the insert operator, although we could do the delete first, it doesn’t really matter too much.

The insert operator works something like this. We enter a position into which we want to insert the string, and then we apply a string that we want to insert. So, in this case, this is again, position zero, one, two and three.

So, I want to insert something at position three, and what I want to insert is simply going to be an I. Now, this could be a string of whatever length I want, I can insert everything here.

I can insert an empty string, although that serves no purpose to me, but I’m just going to insert an I, okay? So, now if I go to print this out, this should have an IE and indeed it does. So that’s step number one, step number two is gonna be to delete the character at this index.

So, I’m going to call name.erase. Now, erase takes a start index as its first argument, and then it takes a length as the second argument. So, in our case here, we want to erase a position, not zero, one, two, three and four. So, we want to erase whatever is starting at position four, okay?

And then we want to insert just a length of one, or rather we just want to erase a length of one. Again, we can have this be whatever length we want, if it reaches the end of the string before the length specified, then that’s just kind of where the function ends.

So, by this point, my string should have my name spelled properly because we’ve inserted the I, and we’ve erased the unwanted E. Okay, so that’s it. I just wanted to showcase these six functions that you will probably find yourself using fairly often with strings.

Like I said, there are a huge number of functions that help us to retrieve properties or modify the string in other ways, most of which I don’t find myself using very often, that’s why I brought these particular six up.

So, feel free to check out the string reference if you’d like, again, linked to that on the slides, if you do want to know more, otherwise I encourage you to try these six operations, these six functions with your own name.

Again, maybe try something like this and see if you can come up with the correct solution. So, that’s it for now, thanks for watching and see you will in the next one.

Interested in continuing?  Check out our all-access plan which includes 250+ courses, guided curriculums, new courses monthly, access to expert course mentors, and more!