substr截取时间段
时间: 2023-11-08 22:03:58 浏览: 159
substr函数是一种用于截取字符串的函数。如果你想要截取一个时间段的子字符串,你可以使用substr函数来实现。
假设你有一个时间字符串如"2022-01-01 12:30:00",你可以使用substr函数来截取其中的年、月、日、时、分、秒等部分。以下是一个示例代码:
```cpp
#include <iostream>
#include <string>
int main() {
std::string timeStr = "2022-01-01 12:30:00";
std::string year = timeStr.substr(0, 4);
std::string month = timeStr.substr(5, 2);
std::string day = timeStr.substr(8, 2);
std::string hour = timeStr.substr(11, 2);
std::string minute = timeStr.substr(14, 2);
std::string second = timeStr.substr(17, 2);
std::cout << "Year: " << year << std::endl;
std::cout << "Month: " << month << std::endl;
std::cout << "Day: " << day << std::endl;
std::cout << "Hour: " << hour << std::endl;
std::cout << "Minute: " << minute << std::endl;
std::cout << "Second: " << second << std::endl;
return 0;
}
```
以上代码会输出以下结果:
```
Year: 2022
Month: 01
Day: 01
Hour: 12
Minute: 30
Second: 00
```
阅读全文