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_director y);
fileDir = CString(strtok(pTemp,"*")) ;
free(pTemp);
Is there an easier way to do this directly to the CString?
CString fileDir;
char* pTemp = strdup((LPCTSTR)m_director
fileDir = CString(strtok(pTemp,"*"))
free(pTemp);
Is there an easier way to do this directly to the CString?
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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);
}
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
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
CString fileDir = CString(m_directory).SpanE
Good luck,
Jaime.