此全局函数来获取特定的源字符串的子字符串。
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);
}
要求
标题: <afxwin.h>