std::string

Introduction

Strings are more or less arrays of characters, containing everything from letters, to numbers, to all of the possible ASCII symbols.

While in C++ you can still print and manipulate strings as plain arrays of characters1, another way is preferred. That's the string class, part of the STL. With it, you can perform many different tasks almost without effort.

How to use it

As with all C++ classes, there are many ways to declare & define strings:

std::string myStr = "Mary had a little lamp.";
std::string myStr2("A tree is not a tree without a trunk.");

Printing the string

The syntax is the same as with all the other types:

std::cout << myStr << std::endl;
std::cout << myStr2 << std::endl;

Find the length of the string

Actually, there are two method for doing that, both doing the same thing. This is because string has inherited traits from other classes that could define those methods to perform differently:

std::cout << "myStr's length is " << myStr.length() << std::endl;
std::cout << "myStr2's length is " << myStr2.size() << std::endl;

Indexing a specific character

Just the same syntax as with arrays:

std::cout << "myStr's 3rd character is " << myStr[2] << std::endl;
std::cout << "myStr2's 9th character is " << myStr2[8] << std::endl;

Remember that similar things happen though, if you try to access outside the string's boundaries.

Adding strings together

Concatenation2 becomes really simple with C++ strings, all you have to do is use the plus( + ) symbol, as you'd do with two integers. The result is a joined string:

std::string myStr3 = myStr + myStr2;
 
std::cout << "myStr + myStr2 = " << myStr3 << std::endl;

More functionality

std::string contains many useful methods, let's take a look at some of them

Clearing the string

The method clear() empties the referred string completely:

myStr2.clear();
std::cout << "myStr2 is: " << myStr2 << std::endl;

Finding if a string is empty

This method, empty(), returns true if the length of the string is zero. For this particular class, it's just a shorthand:

if (myStr2.empty())
    std::cout << "myStr2 is empty." << std::endl;

Find a sequence in a string

The method find() returns the index where it first found the passed string or character. It returns std::string::npos if it can't find it anywhere within the string:

std::cout << myStr.find("lamp") << std::endl; // Searching for string
std::cout << myStr.find('h') << std::endl; // Searching for character

It can also take a second parameter that tells it to start the search from somewhere other than the beginning.

Make a substring of the original

The method substr() returns a substring of the referred string, kind of like the opposite of concatenation. It takes two parameters, the first being the index from which to start the substring, and the second being the desired length of the substring. If no second parameter is given, it takes them all! :D

std::cout << myStr3.substr(0, 23) << std::endl;
std::cout << myStr3.substr(23) << std::endl;

Conclusion

Although the basics have been laid down, there's still much to cover about this class. Maybe you should do your own search if you want to go more in-depth about this. And it's just one class out of over 20 classes, counting only the different types of containers! But don't fret, you don't need to know about every single method of every single class, you just need to know how to read documentation and use them in your program :-)

Have fun coding ;-)

Next tutorial will be about Standard Input. Time to get some feedback from the user!

Unless otherwise stated, the content of this page is licensed under Creative Commons Attribution 3.0 License