Link to home
Start Free TrialLog in
Avatar of galneweinhaw
galneweinhaw

asked on

A CString version of strtok() function

// m_directory hold a directory path with a *. extension in the string, want to cutoff the extension.
CString fileDir;
char* pTemp = strdup((LPCTSTR)m_directory);
fileDir = CString(strtok(pTemp,"*"));
free(pTemp);

Is there an easier way to do this directly to the CString?
ASKER CERTIFIED SOLUTION
Avatar of Jaime Olivares
Jaime Olivares
Flag of Peru image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
In case m_directory is any kind of old-C string (char *), then you need something like this:

CString fileDir = CString(m_directory).SpanExcluding("*");

Good luck,
Jaime.
Avatar of jkr
Check out http://www.codeproject.com/string/tokenizer.asp ("CTokenizer - A simple tokenizer class that can be used on CStrings"). You'd use it like

CTokenizer tok(_T("A-B+C*D-E"), _T("-+"));
CString cs;

But, you simply could use

int nPos = m_directory.ReverseFind('*');

if ( -1 != nPos) { // cut off the rest

m_directory = m_directory.Left(nPos - 1);
}
I think this kind of problems doesn't need any kind of tokenizer function, since you just want to detect a single ocurrence of a character. Due nature of wildcard path, you don't need a reverse find also, because an asterisk could only occur once.
So, SpanExcluding will copy string contents from one CString to another UNTIL an specific character or group of characters is found. Have a look to:
http://msdn.microsoft.com/library/en-us/vcmfc98/html/_mfc_cstring.3a3a.spanexcluding.asp