关闭

string字符串拆分、string转换成int

标签: string
3052人阅读 评论(1) 收藏 举报
分类:
//construct for example: MyDate D5(“2008-1-14 13:5:7”)
MyDate::MyDate(string sDateString)
	{
		string asDate[6]; // sYear, sMonth, sDay, sHour, sMinute, sSecond

		string::size_type uStart = 0;
		string::size_type uEnd = sDateString.find('-');
		// get year and month
		int iCount;
		for (iCount = 0; iCount < 2; iCount++)
		{
			asDate[iCount].assign(sDateString, uStart, (uEnd - uStart));
			uStart = uEnd + 1;
			uEnd = sDateString.rfind('-');
		}
		// get day
		uStart = uEnd + 1;
		uEnd = sDateString.find(' ');
		asDate[iCount++].assign(sDateString, uStart, (uEnd - uStart));
		// get hour and minute
		uStart = uEnd + 1;
		uEnd = sDateString.find(':');
		for (iCount = 3; iCount < 5; iCount++)
		{
			asDate[iCount].assign(sDateString, uStart, (uEnd - uStart));
			uStart = uEnd + 1;
			uEnd = sDateString.rfind(':');
		}
		// get second
		uStart = uEnd + 1;
		uEnd = sDateString.size();
		asDate[iCount].assign(sDateString, uStart, (uEnd - uStart));

		//conversion the string to int
		this->year =  atoi(asDate[0].c_str());
		this->month = atoi(asDate[1].c_str());
		this->day = atoi(asDate[2].c_str());
		this->hour = atoi(asDate[3].c_str());
		this->minute = atoi(asDate[4].c_str());
		this->second = atoi(asDate[5].c_str());
	}

0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:73507次
    • 积分:1004
    • 等级:
    • 排名:千里之外
    • 原创:33篇
    • 转载:17篇
    • 译文:0篇
    • 评论:16条
    文章分类
    最新评论