现在的位置: 首页 > 综合 > 正文

简单字符转换

2017年12月11日 ⁄ 综合 ⁄ 共 2381字 ⁄ 字号 评论关闭

介绍:

主要记录了本人遇到的一点问题及解决方法。

问题概述:

识别类似于 byten_man %2d-%02M-%02d %02h:%02m 中%开头的字符转成相应的时间,转换结果如 byten_man 13-12-25 15:26

解决简述:

主要用到了CString类的一些方法,其他也没有什么难度。

部分源代码如下:

void GetRealString(CString strTemp)
{
    CString strContent, strType;
	int i = 0;
     //当字符串未到达尾部时判断
	while (strTemp.GetAt(i) != '\0')
	{
        //识别%
		if (strTemp.GetAt(i) == '%')
		{
            //%之前的字符保存在m_strContent中
			strContent += strTemp.Left(i);
			//识别数字
            if (strTemp.GetAt(i + 1) >= '0' && strTemp.GetAt(i + 1) <= '9')
			{
                //若%后跟2位数字,如%10d,即取出这段存在strType中
				if (strTemp.GetAt(i + 2) >= '0' && strTemp.GetAt(i + 2) <= '9')
				{
					strType = strTemp.Mid(i, 4);
                   //%之后的字符保存在strTemp中
					strTemp = strTemp.Mid(i + 4);
				}
				else    //若%后跟1位数字,如%2d,即取出这段存在strType中
				{
					strType = strTemp.Mid(i, 3);
                  //%之后的字符保存在strTemp中
					strTemp = strTemp.Mid(i + 3); 
				}
			}
            //识别%后的字符
			else if ((strTemp.GetAt(i + 1) >= 'a' && strTemp.GetAt(i + 1) <= 'z') ||
				(strTemp.GetAt(i + 1) >= 'A' && strTemp.GetAt(i + 1) <= 'Z'))
			{
                //若%后直接跟字符,如%d,则直接取出这段存在strType中
				strType = strTemp.Mid(i, 2);
                //%之后的字符保存在strTemp中
				strTemp = strTemp.Mid(i + 2);
			}
            //字符strTemp的长度一直在变,确保从头开始查找
			i = 0;
            //%之前的字符+识别替换的变量(%d)
			strContent += GetNowTime(strType);
			strType = _T("");       //strType置空
		}
		else	i++;
	}
    //处理完后加上尾部strTemp,strContent即为所要字符串
	strContent += strTemp;
}
CString GetNowTime(CString strType)
{
    CTime ctime = CTime::GetCurrentTime();      //获取当前时间

	CString Type = strType.Right(1);        //取字符,判断是哪种类型
	CString strTemp = _T(""), str;
	int iYearWidth = 0;
	if (strType.GetLength() > 2)
	{
		strTemp = strType.Mid(0, strType.GetLength() - 1);
		CString strYearWidth = strType.Mid(1, strType.GetLength() - 1);
		iYearWidth = StrToInt(strYearWidth);
	}
	else strTemp = strType.Left(1);	

	int iTemp = 0;

/***************识别相应的字符并替换**************/
	if (Type == _T("Y") || Type == _T("y")) //年
	{
		iTemp = ctime.GetYear();
		if (iYearWidth != 0)
		{
			str = ctime.Format(_T("%Y"));
			strType = str.Right(iYearWidth);
			return strType;
		}
	}
	else if (Type == _T("M"))   //月
	{
		iTemp = ctime.GetMonth();
		if ((iTemp == 10) && (m_strReplaceTen != _T("")))
		{
			strType = m_strReplaceTen;
			return strType;
		}
		else if ((iTemp == 11) && (m_strReplaceElv != _T("")))
		{
			strType = m_strReplaceElv;
			return strType;
		}
		else if ((iTemp == 12) && (m_strReplaceTwe != _T("")))
		{
			strType = m_strReplaceTwe;
			return strType;
		}
	}
	else if (Type == _T("D") || Type == _T("d"))    //日
            iTemp = ctime.GetDay();
	else if (Type == _T("W") || Type == _T("w"))    //周
	{
		strType = ctime.Format(_T("%U"));
		int iYear = ctime.GetYear();
        //根据当年的第一天是否是星期天来判断第几周
		CTime ct(iYear, 1, 1, 0, 0, 0); 
		if (ct.GetDayOfWeek() == 1)	iTemp = StrToInt(strType);
		else iTemp = StrToInt(strType) + 1;
	}
	else if (Type == _T("H") || Type == _T("h"))    //时
        iTemp =ctime.GetHour();
	else if (Type == _T("m"))       //分
        iTemp = ctime.GetMinute();

	strTemp += _T("d");
	str.Format(strTemp, iTemp);
	strType = str;

	return strType;
}

结果预览

抱歉!评论已关闭.