国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發設計 > 正文

MFC中幾個有用的字符串操作函數

2019-11-17 05:07:34
字體:
來源:轉載
供稿:網友
MFC中有幾個字符串操作函數很有用,但有的在MSDN中都查不到,因為MSDN沒有公布。下面我們來看看它們的用法和聲明及定義等。

// 功能 : 格式化字符串
// 參數 : rString - 輸出參數,格式化后的字符串將保存在此字符串中
// nIDS - 將進行替換操作的格式字符串的資源ID
// lpsz1 - 指向將替換格式字符串中“%1”字符的字符串
// lpsz2 - 指向將替換格式字符串中“%2”字符的字符串
void AFXAPI AfxFormatString1(CString& rString, UINT nIDS, LPCTSTR lpsz1);
void AFXAPI AfxFormatString2(CString& rString, UINT nIDS, LPCTSTR lpsz1, LPCTSTR lpsz2);
// Implementation string helpers
void AFXAPI AfxFormatStrings(CString& rString, UINT nIDS, LPCTSTR const* rglpsz, int nString);
void AFXAPI AfxFormatStrings(CString& rString, LPCTSTR lpszFormat, LPCTSTR const* rglpsz, int nString);
// 功能 : 獲取子字符串
// 參數 : rString - 輸出參數,保存子字符串
// lpszFullString - 源字符串
// iSubString - 子字符串索引,從0開始計數
// chSep - 子字符串間的分隔字符,默認為'/n'
BOOL AFXAPI AfxExtractSubString(CString& rString, LPCTSTR lpszFullString, int iSubString, TCHAR chSep = '/n');
/////////////////////////////////////////////////
////////////////////////////////////////////////
///
// Strings in format ".....%1 .... %2 ...." etc. void AFXAPI AfxFormatStrings(CString& rString, UINT nIDS,
LPCTSTR const* rglpsz, int nString)
{
TCHAR szFormat[256];
if (!AfxLoadString(nIDS, szFormat) != 0)
{
TRACE1("Error: failed to load AfxFormatString string 0x%04x./n", nIDS);
ASSERT(FALSE);
return;
}
AfxFormatStrings(rString, szFormat, rglpsz, nString);
}

void AFXAPI AfxFormatStrings(CString& rString, LPCTSTR lpszFormat,
LPCTSTR const* rglpsz, int nString)
{
// 計算結果字符串的長度
int nTotalLen = 0;
LPCTSTR pchSrc = lpszFormat;
while (*pchSrc != '/0')
{
if (pchSrc[0] == '%' &&
( (pchSrc[1] >= '0' && pchSrc[1] <= '9')
(pchSrc[1] >= 'A' && pchSrc[1] <= 'Z')) )
{
// %A comes after %9 -- we'll need it someday
int i;
if (pchSrc[1] >'9')
i = 9 + (pchSrc[1] - 'A');
else
i = pchSrc[1] - '1';
pchSrc += 2;
if (i >= nString)
++nTotalLen;
else if (rglpsz[i] != NULL)
nTotalLen += lstrlen(rglpsz[i]);
}
else
{
if (_istlead(*pchSrc))
++nTotalLen, ++pchSrc;
++pchSrc;
++nTotalLen;
}
}

pchSrc = lpszFormat;
LPTSTR pchDest = rString.GetBuffer(nTotalLen);
while (*pchSrc != '/0')
{
if (pchSrc[0] == '%' &&
( (pchSrc[1] >= '0' && pchSrc[1] <= '9')
(pchSrc[1] >= 'A' && pchSrc[1] <= 'Z')) )
{
// %A comes after %9 -- we'll need it someday
int i;
if (pchSrc[1] >'9')
i = 9 + (pchSrc[1] - 'A');
else
i = pchSrc[1] - '1';

pchSrc += 2;
if (i >= nString)
{
TRACE1("Error: illegal string index requested %d./n", i);
*pchDest++ = '?';
}
else if (rglpsz[i] != NULL)
{
lstrcpy(pchDest, rglpsz[i]);
pchDest += lstrlen(pchDest);
}
}
else
{
if (_istlead(*pchSr
c)) // *pchSrc是否多字節字符的首字節
*pchDest++ = *pchSrc++; // 拷貝首字節
*pchDest++ = *pchSrc++;
}
}
rString.ReleaseBuffer((int)((LPCTSTR)pchDest - (LPCTSTR)rString));
// ReleaseBuffer will assert if we went too far
}

void AFXAPI AfxFormatString1(CString& rString, UINT nIDS, LPCTSTR lpsz1)
{
AfxFormatStrings(rString, nIDS, &lpsz1, 1);
}

void AFXAPI AfxFormatString2(CString& rString, UINT nIDS, LPCTSTR lpsz1,
LPCTSTR lpsz2)
{
LPCTSTR rglpsz[2];
rglpsz[0] = lpsz1;
rglpsz[1] = lpsz2;
AfxFormatStrings(rString, nIDS, rglpsz, 2);
}

BOOL AFXAPI AfxExtractSubString(CString& rString, LPCTSTR lpszFullString,
int iSubString, TCHAR chSep)
{
if (lpszFullString == NULL)
return FALSE;

while (iSubString--)
{
lpszFullString = _tcschr(lpszFullString, chSep);
if (lpszFullString == NULL)
{
rString.Empty(); // return empty string as well
return FALSE;
}
lpszFullString++; // point past the separator
}
LPCTSTR lpchEnd = _tcschr(lpszFullString, chSep);
int nLen = (lpchEnd == NULL) ? lstrlen(lpszFullString) : (int)(lpchEnd - lpszFullString);
ASSERT(nLen >= 0);
memcpy(rString.GetBufferSetLength(nLen), lpszFullString, nLen*sizeof(TCHAR));
return TRUE;
}
/////////////////////////////////////////////////
////////////////////////////////////////////////
///


發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 广东省| 星子县| 泸水县| 上饶市| 塔城市| 秦皇岛市| 公主岭市| 杂多县| 修文县| 乐昌市| 宝兴县| 体育| 磐安县| 宜昌市| 饶平县| 阿瓦提县| 英吉沙县| 金山区| 洛川县| 丰顺县| 灌南县| 扶风县| 黔江区| 永和县| 江达县| 临漳县| 玉屏| 馆陶县| 耒阳市| 旬阳县| 贵定县| 绿春县| 九江市| 杭锦旗| 泰顺县| 乌拉特中旗| 涟源市| 崇左市| 晋宁县| 晋宁县| 红安县|