AfxExtractSubString
這個全域函式可以從指定的來源字串擷取子字串。
BOOL AFXAPI AfxExtractSubString (
CString& rString,
LPCTSTR lpszFullString,
int iSubString,
TCHAR chSep = '\n'
);
參數
rString
- 要接收一條個別子字串之 CString 物件的參考。
lpszFullString
- 字串包含字串的全文擷取自。
iSubString
- 擷取的子字串之以零起始的索引。 lpszFullString。
chSep
- 用來分隔符號分隔的子字串。
傳回值
TRUE ,如果函式成功擷取的子字串在提供的索引;否則, FALSE。
備註
表示已知的單一字元分隔每個子字串時,這個函式會擷取多子字串是有用的來源字串。 每次呼叫方法時,這個函式,搜尋 lpszFullString 前端的參數。
這個函式會傳回 false,或者如果 lpszFullString 設定為 NULL 或函式到達 lpszFullString 結尾,但找不到 iSubString指定的分隔符號字元的 +1 事件。 如果 lpszFullString 設定為 NULL, rString 參數不會從其原始值修改;則為,否則為,表示子字串無法為指定的索引,擷取 rString 參數設定為空字串。
範例
// The following example extracts a series of name, value pairs from a
// given source string:
// Input string consisting of a number of name, value pairs
LPCTSTR lpszSource = _T("\"Name\"=\"John Smith\"\n")
_T("\"Company\"=\"Contoso, Ltd\"\n\"Salary\"=\"25,000\"");
CString strNameValue; // an individual name, value pair
int i = 0; // substring index to extract
while (AfxExtractSubString(strNameValue, lpszSource, i))
{
// Prepare to move to the next substring
i++;
CString strName, strValue; // individual name and value elements
// Attempt to extract the name element from the pair
if (!AfxExtractSubString(strName, strNameValue, 0, _T('=')))
{
// Pass an error message to the debugger for display
OutputDebugString(_T("Error extracting name\r\n"));
continue;
}
// Attempt to extract the value element from the pair
if (!AfxExtractSubString(strValue, strNameValue, 1, _T('=')))
{
// Pass an error message to the debugger for display
OutputDebugString(_T("Error extracting value element\r\n"));
continue;
}
// Pass the name, value pair to the debugger for display
CString strOutput = strName + _T(" equals ") + strValue + _T("\r\n");
OutputDebugString(strOutput);
}
需求
Header: <afxwin.h>