C++ Standard Library <string>
The C++ Standard Library (Standard Template Library, STL) is one of the core components of C++, providing rich data structures and algorithms.
<string>It is the header file in the C++ Standard Library used for handling strings.
In C++, a string is a sequence of characters.<string>The header file providesstd::stringIt is a class that encapsulates C-style strings, providing safer and easier-to-use string manipulation functionality.
To use in a C++ program<string>the library, you first need to include this header file:
#include <iostream> #include <string>
Basic Syntax
std::stringThe basic syntax of the class is as follows:
-
Declare a string variable:
std::string str;
-
Initialize string:
std::string str = "Hello, World!";
-
Usage
+Concatenate strings:std::string str1 = "Hello, "; std::string str2 = "World!"; std::string result = str1 + str2;
Common member functions
std::stringThe class provides many member functions for manipulating strings. The following are some commonly used member functions:
size(): Returns the length of the string.empty(): Checks whether the string is empty.operator[]Access a character in the string by index.substr(): Gets a substring.find()Find the position of a substring in the main string.replace(): Replaces some characters in the string.
Example
Below is a simple example using the<string>library, including the output results:
Example
#include <string>
int main() {
// Declare and initialize a string
std::string greeting = "Hello, World!";
std::cout << "Greeting: " << greeting << std::endl;
// Use size() to get the string length
std::cout << "Length of the greeting: " << greeting.size() << std::endl;
// Use empty() to check if the string is empty
std::cout << "Is the greeting empty? " << (greeting.empty() ? "Yes" : "No") << std::endl;
// Use operator[] to access a character at a specific position
std::cout << "Character at position 7: " << greeting[7] << std::endl;
// Use substr() to get a substring
std::string sub = greeting.substr(7, 5);
std::cout << "Substring from position 7 with length 5: " << sub << std::endl;
// Use find() to find a substring
std::cout << "Position of 'World' in the greeting: " << greeting.find("World") << std::endl;
// Use replace() to replace part of the string
// Replace 'World' with 'C++'
std::string modified = greeting;
std::string::size_type pos = modified.find("World");
if (pos != std::string::npos) {
modified.replace(pos, 5, "C++"); // Starting from position pos, replace 5 characters with "C++"
}
std::cout << "Modified greeting: " << modified << std::endl;
return 0;
}
Output:
Length of the greeting: 13 Is the greeting empty? No Character at position 7: W Substring from position 7 with length 5: World Position of 'World' in the greeting: 7 Modified greeting: Hello, C++!
std::string member function summary table
The following is a summary of common std::string member functions:| Function Name | Description | Example code |
|---|---|---|
size() | Returns the length of the string (the number of characters). | std::cout << str.size(); |
length() | andsize()Same, returns the length of the string. | std::cout << str.length(); |
empty() | Determines whether the string is empty. | std::cout << (str.empty() ? "Yes" : "No"); |
operator[] | Accesses the character at a specified position in the string. | std::cout << str[0]; |
at() | Access the character at a specified position in the string (with bounds checking). | std::cout << str.at(0); |
substr() | Return a substring starting from the specified position. | std::string sub = str.substr(0, 5); |
find() | Find the position of a substring in the string. | std::cout << str.find("sub") << std::endl; |
rfind() | Find the position of a substring starting from the end of the string. | std::cout << str.rfind("sub") << std::endl; |
replace() | Replaces part of the content in the string. | str.replace(pos, length, "new_substring"); |
append() | Adds content at the end of the string. | str.append(" more"); |
insert() | Inserts content at a specified position. | str.insert(pos, "inserted"); |
erase() | Delete the character or substring at the specified position. | str.erase(pos, length); |
clear() | Clear the string. | str.clear(); |
c_str() | Return a C-style string (null-terminated). | const char* cstr = str.c_str(); |
data() | Return a pointer to the character data (C++11 and later). | const char* data = str.data(); |
compare() | Compares two strings. | int result = str.compare("other"); |
find_first_of() | Find the position of the first character that matches any of the specified characters. | size_t pos = str.find_first_of("aeiou"); |
find_last_of() | Find the position of the last character that matches any of the specified characters. | size_t pos = str.find_last_of("aeiou"); |
find_first_not_of() | Find the position of the first character that does not match any of the specified characters. | size_t pos = str.find_first_not_of("aeiou"); |
find_last_not_of() | Find the position of the last character that does not match any of the specified characters. | size_t pos = str.find_last_not_of("aeiou"); |
Example
#include <string>
int main() {
std::string str = "Hello, World!";
// size()
std::cout << "Length: " << str.size() << std::endl;
// empty()
std::cout << "Is empty? " << (str.empty() ? "Yes" : "No") << std::endl;
// operator[]
std::cout << "First character: " << str[0] << std::endl;
// at()
std::cout << "Character at position 7: " << str.at(7) << std::endl;
// substr()
std::string sub = str.substr(7, 5);
std::cout << "Substring from position 7 with length 5: " << sub << std::endl;
// find()
size_t pos = str.find("World");
std::cout << "Position of 'World': " << pos << std::endl;
// replace()
str.replace(pos, 5, "C++");
std::cout << "Modified string: " << str << std::endl;
// append()
str.append(" How are you?");
std::cout << "Appended string: " << str << std::endl;
// insert()
str.insert(7, " Beautiful");
std::cout << "String after insert: " << str << std::endl;
// erase()
str.erase(7, 10);
std::cout << "String after erase: " << str << std::endl;
// clear()
str.clear();
std::cout << "String after clear: " << (str.empty() ? "Empty" : "Not empty") << std::endl;
// c_str()
str = "Hello, C++!";
const char* cstr = str.c_str();
std::cout << "C-style string: " << cstr << std::endl;
// compare()
int cmp = str.compare("Hello, C++!");
std::cout << "Comparison result: " << cmp << std::endl;
// find_first_of()
size_t pos_first_vowel = str.find_first_of("aeiou");
std::cout << "First vowel at position: " << pos_first_vowel << std::endl;
// find_last_of()
size_t pos_last_vowel = str.find_last_of("aeiou");
std::cout << "Last vowel at position: " << pos_last_vowel << std::endl;
// find_first_not_of()
size_t pos_first_non_vowel = str.find_first_not_of("aeiou");
std::cout << "First non-vowel at position: " << pos_first_non_vowel << std::endl;
// find_last_not_of()
size_t pos_last_non_vowel = str.find_last_not_of("aeiou");
std::cout << "Last non-vowel at position: " << pos_last_non_vowel << std::endl;
return 0;
}
Output:
Length: 13 Is empty? No First character: H Character at position 7: W Substring from position 7 with length 5: World Position of 'World': 7 Modified string: Hello, C++! Appended string: Hello, C++! How are you? String after insert: Hello, BeautifulC++! How are you? String after erase: Hello, C++! How are you? String after clear: Empty C-style string: Hello, C++! Comparison result: 0 First vowel at position: 1 Last vowel at position: 4 First non-vowel at position: 0 Last non-vowel at position: 10other extensions