[英]How to get a particular part of string in c++?
我有以下字符串,我需要取出 id (68890) 類似日期 (01/14/2005)
CString strsample = "This is demo to capture the details of date (01/14/2005) parent id (68890) read (0)"
CString strsample = This is demo (Sample,Application) to capture the details of date (01/14/2005) parent id (68890) read (0) Total(%5)
我如何在 C++ 中實現這一點,是否有任何方法可以在正則表達式或任何可用功能中執行。
使用標准庫最簡單的方法是使用std::sscanf :
#include <iostream>
#include <string>
#include <cstdio>
#include <ctime>
#include <iomanip>
int main()
{
std::string s;
while (std::getline(std::cin, s)) {
std::tm t{};
int id, read;
auto c = std::sscanf(s.c_str(),
"This is demo to capture the details of date (%d/%d/%d) parent id (%d) read (%d)",
&t.tm_mon,
&t.tm_mday,
&t.tm_year,
&id,
&read);
--t.tm_mon;
t.tm_year -= 1900;
std::mktime(&t);
if (c == 5) {
std::cout << std::put_time(&t, "%F")
<< " id: " << id
<< " read: " << read
<< '\n';
} else {
std::cerr << "Invalid input\n";
}
}
return 0;
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.