In C++, the std::string
class is a part of the C++ Standard Library (STL) and provides a convenient way to work with strings. It is a dynamic array of characters that automatically manages memory and provides various member functions to manipulate and access strings efficiently. Unlike C-style strings, std::string
objects can grow or shrink dynamically as needed, making them more flexible and less prone to buffer overflows.
Here’s an explanation of some important functions of the std::string
class along with examples:
Table of Contents
Declaration and Initialization:
- You can declare and initialize an
std::string
in different ways:
#include <iostream>
#include <string>
int main() {
std::string str1; // Empty string
std::string str2 = "Hello, World!"; // Initialization with a C-string
std::string str3("Welcome to C++"); // Initialization with a C-string using constructor
std::string str4 = str2; // Copy constructor
std::cout << "str2: " << str2 << std::endl;
std::cout << "str3: " << str3 << std::endl;
std::cout << "str4: " << str4 << std::endl;
return 0;
}
Length of the String:
- The
length()
orsize()
member function is used to get the length of the string.
#include <iostream>
#include <string>
int main() {
std::string str = "Hello, World!";
std::cout << "Length of the string: " << str.length() << std::endl;
return 0;
}
Concatenation:
- Strings can be concatenated using the
+
operator or theappend()
member function.
#include <iostream>
#include <string>
int main() {
std::string str1 = "Hello, ";
std::string str2 = "World!";
std::string result = str1 + str2; // Concatenation using operator
// std::string result = str1.append(str2); // Concatenation using append()
std::cout << "Concatenated string: " << result << std::endl;
return 0;
}
Substrings:
- The
substr()
member function is used to extract a substring from a string.
#include <iostream>
#include <string>
int main() {
std::string str = "Hello, World!";
std::string sub = str.substr(7, 5); // Extract "World" starting from index 7, with length 5
std::cout << "Substring: " << sub << std::endl;
return 0;
}
Searching:
- The
find()
member function is used to search for a substring within a string.
#include <iostream>
#include <string>
int main() {
std::string str = "Hello, World!";
size_t found = str.find("World");
if (found != std::string::npos) {
std::cout << "Substring found at index: " << found << std::endl;
} else {
std::cout << "Substring not found." << std::endl;
}
return 0;
}
Character Access:
- Individual characters in the string can be accessed using the
[]
operator.
#include <iostream>
#include <string>
int main() {
std::string str = "Hello";
char ch = str[1]; // Accessing the character at index 1 (e.g., 'e')
std::cout << "Character at index 1: " << ch << std::endl;
return 0;
}
Input and Output:
std::string
can be easily input and output using standard streams.
#include <iostream>
#include <string>
int main() {
std::string name;
std::cout << "Enter your name: ";
std::getline(std::cin, name);
std::cout << "Hello, " << name << "!" << std::endl;
return 0;
}
The std::string
class provides many other useful member functions like erase()
, replace()
, compare()
, etc., which offer extensive functionality for string manipulation and handling. Using std::string
is recommended in C++ as it is safer, more flexible, and provides a wide range of features to work efficiently with strings.