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 個特定的分隔字元。rString參數將不會修改從其原始值如果lpszFullString被設定為 NULL。 否則, 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>

請參閱

概念

MFC 巨集和全域變數

其他資源

Cstring