MFC/유용한 코드2012.05.15 16:25

출처 :

MFC 프로그램 처리시 가끔 문자열에 대해 Token 처리해야 하는 경우가 발생합니다. 이때 token을 위한 buf를 만들고 해제하는 과정이 아주 귀찮습니다.. 이때 사용할수 있는 초 간단 Token 처리 API입니다. 

CString strOrg;
CString strTok_1;
CString strTok_2;
CString strTok_3;

strOrg.Format("%s", "123$456$789");

AfxExtractSubString( strTok_1, strOrg, 0, '$');
AfxExtractSubString( strTok_2, strOrg, 1, '$');
AfxExtractSubString( strTok_3, strOrg, 2, '$');

strTok_1에는 "123"이
strTok_2에는 "456"이
strTok_3에는 "789"가 대입됩니다..

참조 :

MFC Library Reference 

This global function can be used to extract a substring from a given source string.

: <afxwin.h>

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


  • Reference to a CString object that will receive an individual substring.

  • String containing the full text of the string to extract from.

  • Zero-based index of the substring to extract from lpszFullString.

  • Separator character used to delimit substrings.

TRUE if the function successfully extracted the substring at the provided index; otherwise, FALSE.

This function is useful for extracting multiple substrings from a source string when a known single character separates each substring. This function searches from the beginning of the lpszFullString parameter each time it is called.

This function will return FALSE if either lpszFullString is set to NULL or the function reaches the end oflpszFullString without finding iSubString+1 occurrences of the specified separator character. The rString parameter will not be modified from its original value if lpszFullString was set to NULL; otherwise, the rString parameter is set to the empty string if the substring could not be extracted for the specified index.

Posted by jangjeonghun

티스토리 툴바