Partager via


AfxExtractSubString

Cette fonction d'agrégation peut être utilisée pour extraire une sous-chaîne d'une chaîne source donnée.

BOOL AFXAPI AfxExtractSubString ( 
   CString& rString, 
   LPCTSTR lpszFullString, 
   int iSubString, 
   TCHAR chSep = '\n' 
);

Paramètres

  • string

    • Référence à un objet d'CString qui recevra une sous-chaîne individuelle.
  • lpszFullString

    • Chaîne contenant le texte intégral de la chaîne à extraire.
  • subString

    • Index de base zéro de la sous-chaîne à extraire de lpszFullString.
  • chSep

    • Caractère de séparation utilisé pour délimiter des sous-chaînes.

Valeur de retour

TRUE si la fonction extrayait correctement la sous-chaîne à l'index spécifié ; sinon, FALSE.

Notes

Cette fonction est utile pour extraire plusieurs sous-chaînes d'une chaîne source lorsqu'un caractère sépare chaque sous-chaîne. Recherches de cette fonction début du paramètre d'lpszFullString chaque fois qu'elle est appelée.

Cette fonction retourne FALSE si ou lpszFullString a la valeur NULL ou la fonction atteint la fin de lpszFullString sans rechercher iSubString+1 occurrences du caractère de séparation spécifié. Le paramètre d'rString ne sera pas modifié sa valeur d'origine lpszFullString si est défini à NULL; sinon, le paramètre d'rString est défini sur une chaîne vide si la sous-chaîne ne peut pas être extraite à l'index spécifié.

Exemple

// 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);
}

Configuration requise

En-tête: <afxwin.h>

Voir aussi

Concepts

macro MFC et Globals

Autres ressources

À l'aide de CString