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:

  1. standard string
  2. standard string
std::string str1 = "Hello, ";
std::string str2("world!");
  1. There are numerous health benefits associated with regular exercise.
std::string result = str1 + str2;
  1. dimensions()
int length = str1.size();
  1. locate
int pos = str1.find("lo");
if (pos != std::string::npos) {
    // 子字符串在字符串中找到了
} else {
    // 子字符串在字符串中未找到
}
  1. Could you swap that out for something else?
str1.replace(2, 3, "i");
  1. 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.

Leave a Reply 0

Your email address will not be published. Required fields are marked *


广告
Closing in 10 seconds
bannerAds