본문 바로가기
프로그래밍/C++

[MFC] CString 문자열 분리

by 정리 습관(★arranging★) 2019. 10. 25.
728x90

CString 문자열 분리 두가지 사용 방법과 차이점

1. AfxExtractSubString : https://docs.microsoft.com/en-us/cpp/mfc/reference/cstring-formatting-and-message-box-display?view=vs-2019#afxextractsubstring

 

CString Formatting and Message-Box Display

CString Formatting and Message-Box Display In this article --> A number of functions are provided to format and parse CString objects. You can use these functions whenever you have to manipulate CString objects, but they are particularly useful for formatt

docs.microsoft.com

함수 원형

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

2. Tokenize : Ahttps://social.msdn.microsoft.com/Forums/en-US/99d073d4-83da-4bee-82b4-1825270e3c1f/cstringtokenize?forum=vcmfcatl

 

CString::Tokenize

Yes, i did tried it on a test project. It works fine. however, when i put it inside my codes, it gives me the assert error. this is where it happens: ATLASSERT( iStart >= 0 ); When i put breakpoints at the while loop, i was able to see that i get all the d

social.msdn.microsoft.com

CString Seperator = _T("\t");
int Position = 0;
CString Token;

Token = sBuf.Tokenize(Seperator, Position);
	while(!Token.IsEmpty())
	{
		// Get next token.
		Token = sBuf.Tokenize(Seperator, Position);
	} 

 

예제 ) '/' 로 구분되는 문자에 대해 분리 vecText 벡터에 적재

 

vector<CString>	vecText;
CString strText(_T(""));		
//1. AfxExtractSubString 사용
int nP=0;
while (FALSE != AfxExtractSubString(strText, pText->GetText(), nP++, _T('/')))
{
	vecText.push_back(strText);
}

//2. Tokenize 사용
nP=0;
strText = pText->GetText().Tokenize(_T("/"), nP);
while(!strText.IsEmpty())
{				
	strText = pText->GetText().Tokenize(_T("/"), nP);
	vecText.push_back(strText);
}
if ( 0 == nP )
	vecText.push_back(pText->GetText());

AfxExtractSubString 는 파라메터에서 레퍼런스로 분리결과 전달,

Tokenize 는 분리결과를 리턴해서 전달.

AfxExtractSubString 나 Tokenize 나 거기서 거기 사용법만 다르지 똑같다.

 

구분자인 delimiter 값이 없을때도

AfxExtractSubString 리턴값 TRUE / FALSE 는 마지막까지 갔냐의 의미로 봄 'TRUE' 레퍼런스 변수에 해당 문자 그대로 들어옮

Tokenize 리턴값으로 해당 문자 그대로 들어옮

 

내부 동작이나 성능은 확인필요...

댓글