C++ Tutorial for noobs - Part 10: Strings
Strings are a data-type that can hold words, letters, sentences or any combination of those. C++ comes with a String data-type that is very easy to use and has a few very useful functions that we will explain in this part of the C++ Tutorial for noobs.
char*
The default 'string' data-type in C++ is char*, or in other words an Array of type char. For example, this is how we create a char* string:
char* s = "I love noobtuts.com";
Note: the * stands for the array size that will be detected automatically. We could also write char[19] to create a char array that holds the text 'I love noobtuts.com'.
Now there are two big problems with char*. Because it's an array, this means that:
- it has a fixed size, hence we can't just add things to it
- we never really know the size for sure (it has no .size() function that we can call)
So while char* kinda works, it's really not recommended to use it for strings.
std::string
Creating a string
What we are really looking for is the std::string, which can be used like this:
#include <string>
std::string s = "I love noobtuts.com";
std::cout<<s;
Adding things to a string
The cool thing about std::string is that it solves the two problems mentioned above. At first, we can easily add things to the end of a string like so:
std::string s = "I love noobtuts.com";
s = s + 'X';
which results in 'I love noobtuts.comX'.
String size
Furthermore, std::string also keeps track of the size, or in other words: the amount of letters in the string:
std::string s = "abc";
std::cout<<s.size(); // gives out 3
Accessing a letter in a string
Similar to Arrays and lists, we can also access every element in our string:
std::string s = "abc";
std::cout<<s[0]; // gives out 'a'
std::cout<<s[1]; // gives out 'b'
std::cout<<s[2]; // gives out 'c'
It's important to remember that the elements inside a string are always letters, which means that they are the C++ char data-type.
Note: in the end, a string is really just a list of type char.
Looping over a string
Since we have a size() function we can also use a for-loop with a string very easily:
std::string s = "abc";
for (int i = 0; i < s.size(); ++i) {
std::cout<<s[i];
}
Special Functions
Almost as if this is not good enough yet, the std::string also comes with a few really cool functions. Let's take a look at the most important ones.
Substring
For example, we can use the substr function to copy out a part of the string like so:
std::string s = "abc";
std::cout<<s.substr(0, 2); // gives out 'ab'
Note: the two parameters of the substr method are the beginning index and the length.
Compare
Another useful, yet kinda confusing function is compare. Sadly we can't just check for string equality with ==:
std::string a = "abc";
std::string b = "bcd";
if (a == b) {
std::cout<<"equal!";
}
because == compares the string objects and not the string contents. This is not very easy to understand, but there is a easy solution to it, which is the compare function. The function compares one string with another, and if it returns 0 this means that they are equal:
std::string a = "abc";
std::string b = "bcd";
if (a.compare(b) == 0) {
std::cout<<"equal!";
}