cpp / beginner
Snippet
Introduction to std::string
The std::string class from the C++ standard library allows you to work with text. You can concatenate strings using +, access their length, and easily manipulate text.
snippet.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
#include <iostream>#include <string>using namespace std;int main() {string greeting = "Hello";string name = "World";string message = greeting + ", " + name + "!";cout << message << endl;cout << "Length: " << message.length() << endl;return 0;}
Breakdown
1
#include <string>
Required header to use std::string
2
string greeting = "Hello";
Creates a string variable holding text
3
greeting + ", " + name + "!"
Concatenates multiple strings together into one
4
message.length()
Returns the number of characters in the string