C++ Strings

<string>
C++
...
using string = basic_string<char>;

So, what’s a basic_string? In short, this is just a class that generalizes the concept of a string; at their core, strings are just sequential collections of character-like objects. In our case, the std::string is just a sequential collection of objects with type char.

Now that we’ve gotten the definition of a string established, let’s briefly explore some of the properties of strings, namely the most commonly used string operators and methods.

Let’s create a simple string and try to print it to stdout:

printing_strings.cpp
C++
#include <iostream>
#include <string>
int main() {
// Create a string
std::string hello = "Hello, world!";
std::cout << hello;
return 0;
}

Let’s compile and run this to see what we get:

❯ g++ printing_strings.cpp
❯ ./a.out
Hello, world!

We see the text that we stored in the string printed to the terminal, just as we should expect.

Let’s look at an interesting property of strings: we can index into them to retrieve individual characters and even substrings!

string_indexing.cpp
C++
#include <iostream>
#include <string>
int main() {
// Create a string
std::string index_me = "Hello, world!";
std::cout << index_me[4];
return 0;
}

Remember, a string is really just an array of characters, so we can index into it like in the above example to retrieve the character at the specified index. When running the code above, we should expect to see the character at index 4, which is the letter ‘o’, printed to the terminal. Let’s see if that is the case.

❯ g++ string_indexing.cpp
❯ ./a.out
o

Lo and behold, we see the letter ‘o’ printed to the terminal just as we would expect. If we wanted to grab a substring, we can use the built-in substr method.

substrings.cpp
C++
#include <iostream>
#include <string>
int main() {
// Create a string
std::string slice_me = "Hello, world!";
std::cout << slice_me.substr(7, 5);
return 0;
}
❯ g++ substrings.cpp
❯ ./a.out
world

Strings can also undergo an operation known as concatenation. Concatenation is when multiple strings are chained together to create one larger string. In C++, concatenation can be accomplished using the + operator. Let’s look at an example.

concat.cpp
C++
#include <iostream>
#include <string>
int main() {
// Create a couple strings
std::string str1 = "Hel";
std::string str2 = "lo, wo";
std::string str3 = "rld!";
// Create a new string by concatenation of the previous strings
std::string full_string = str1 + str2 + str3;
// Verify the new string
std::cout << full_string;
return 0;
}

We define three strings, and then we initialize another string by assigning it the result of the concatenation of the previous strings.

❯ g++ concat.cpp
❯ ./a.out
Hello, world!

Strings can also be concatenated via the append method:

append.cpp
C++
#include <iostream>
#include <string>
int main() {
// Create a couple strings
std::string str1 = "Hello, ";
std::string str2 = "world!";
str1.append(str2);
std::cout << full_string;
return 0;
}
❯ g++ concat.cpp
❯ ./a.out
Hello, world!

Another interesting property of strings is that we can query information about their size. In fact, strings in C++ possess some methods similar to those of std::vector. Let’s look at some of the methods available for gathering information about the size of a string:

string_size.cpp
C++
#include <iostream>
#include <string>
int main() {
// Create a string
std::string example = "Let's use a different example";
// Query size and length, which are synonymous for strings
std::cout << "Size of string: " << example.size() << "\n";
std::cout << "Length of string: " << example.length() << "\n";
// Check if string is empty
std::cout << "String is empty: " << std::boolalpha << example.empty();
return 0;
}

The size and length methods are synonymous, meaning we should see the same result for both. There is also the empty method which checks if the string is empty and returns a bool. If we compile and run, we should see that the size and the length of the string is 29 (because there are 29 characters in the string) and we should also see that the string is not empty (i.e., we should see false).

❯ g++ string_size.cpp
❯ ./a.out
Size of string: 29
Length of string: 29
String is empty: false

And that is exactly what we see!

If you enjoy this type of content and want to stay in the know when I post new stuff, use the dialog below this post to subscribe to stay informed.

  1. https://en.wikipedia.org/wiki/Comparison_of_programming_languages_(strings) ↩︎
  2. https://cplusplus.com/reference/string/string/ ↩︎
  3. https://cplusplus.com/reference/string/string/substr/ ↩︎

Discover more from shared_ptr

Subscribe now to keep reading and get access to the full archive.

Continue reading