【发布时间】:2015-05-11 14:47:37
【问题描述】:
假设我有一个简单的字符串:
string example = "phone number: XXXXXXX,"
其中 X 是给我的随机值,所以它们总是不同的。
如何只提取 X?
"phone number: " 不会改变。
我现在拥有的(使用下面的 Thomas Matthews 技术)
const char search_text[] = "phone number: ";
std::string::size_type start_posn = example.find(search_text);
std::string::size_type end_posn = example.find(",", start_posn);
const unsigned int length = end_posn - start_posn - sizeof(search_text) - 1;
cout << "length: " << length << endl;
std::string data = example.substr(start_posn, length);
cout << data << endl;
如果我有string example = "phone number: XXXXXXX, Date: XXXXXXXX,"呢?
【问题讨论】:
-
看看[正则表达式] (cplusplus.com/reference/regex/regex_search)
-
在发布您的问题之前先搜索 stackoverflow.com:stackoverflow.com/…