我有一个关于从 C++ 字符串中删除整数的基本问题。我不是特别熟悉 C++,也不是很熟悉正则表达式。
我有一个字符串,例如
string myString = "12 text 345 text"
整数可以是任意长度,文本也可以(不固定)。我需要做的是将第一个整数 (12) 和第二个整数 (345) 提取到两个单独的整数对象中。
如有任何帮助,我们将不胜感激。
最佳答案
您可以按如下方式使用stringstream
:
std::string myString = "12 text 345 text";
int firstInteger = 0;
int secondInteger = 0;
std::string firstString= "";
std:string secondString="";
std::stringstream ss(myString);
ss >> firstInteger >> firstString >> secondInteger >> secondString;
std::cout << "firstInteger " << firstInteger
<< "\nSecondInteger " << secondInteger <<std::endl;
输出:
firstInteger 12
secondInteger 345
关于c++ - 提取字符串中出现的前两个整数 C++,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15777716/