C++字符串切割主要用到string的两个函数 find()和substr(),先用find函数找到需要切割的位置,再用substr进行切割,示例程序如下:
#include#include using namespace std; int main() { string str = "Hello World!"; cout << str << endl; string::size_type comper = str.find(" "); string a = str.substr(0, comper); cout << a << endl; string b = str.substr(comper+1, str.size()); cout << b << endl; }
判断字符串中是否包含某个子串:
#include#include using namespace std; int main() { string str = "Hello World!"; cout << str << endl; string::size_type comper = str.find("Hello"); if (comper != string::npos) { cout << "str中包含Hello"; } else { cout << "str中不包含Hello"; } }



