How to handle strings in C++?
In C++, the std::string class can be used to manipulate strings. This class offers many useful member functions for string operations, such as:
- standard string
- standard string
std::string str1 = "Hello, ";
std::string str2("world!");
- There are numerous health benefits associated with regular exercise.
std::string result = str1 + str2;
- dimensions()
int length = str1.size();
- locate
int pos = str1.find("lo");
if (pos != std::string::npos) {
// 子字符串在字符串中找到了
} else {
// 子字符串在字符串中未找到
}
- Could you swap that out for something else?
str1.replace(2, 3, "i");
- extracts a part of a string
std::string sub = str1.substr(2, 3);
In addition to the member functions mentioned above, the std::string class also offers many other useful member functions for manipulating strings. For detailed usage, refer to the C++ standard library documentation.