C++ 字符串提取:从 'with' 到 'pick' 之间的文本
"You have 2 parcels to Cainiao Station. Please with 10-1-3125, 1-5-4453 pick them up before 17:00." 如何只读取with和pick之间的字符 c++ 要实现只读取"with"和"pick"之间的字符,可以使用C++中的字符串处理函数。下面是一个示例代码: \n\nc++\n#include <iostream>\n#include <string>\n\nint main() {\n std::string input = \"Please with 10-1-3125, 1-5-4453 pick them up before 17:00.\";\n \n // 找到\"with\"和\"pick\"的位置\n size_t startPos = input.find(\"with\") + 4; // 4是\"with\"的长度\n size_t endPos = input.find(\"pick\");\n\n // 提取\"with\"和\"pick\"之间的子字符串\n std::string result = input.substr(startPos, endPos - startPos);\n\n // 输出结果\n std::cout << result << std::endl;\n\n return 0;\n}\n\n\n输出结果为:\n\n\n10-1-3125, 1-5-4453\n\n\n这段代码使用find函数找到"with"和"pick"的位置,然后使用substr函数提取两者之间的子字符串。
原文地址: https://www.cveoy.top/t/topic/pHPE 著作权归作者所有。请勿转载和采集!