A CString version of strtok() function

Posted on 2004-11-09
Last Modified: 2013-11-20
// 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,"*"));

Is there an easier way to do this directly to the CString?
Question by:galneweinhaw
    LVL 55

    Accepted Solution

    Assuming m_directory is also CString type, just you need:

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

    Expert Comment

    by:Jaime Olivares
    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,
    LVL 86

    Expert Comment

    Check out ("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);
    LVL 55

    Expert Comment

    by:Jaime Olivares
    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:

    Featured Post

    Looking for New Ways to Advertise?

    Engage with tech pros in our community with native advertising, as a Vendor Expert, and more.

    Join & Write a Comment

    Suggested Solutions

    Title # Comments Views Activity
    Need to retreive data from .TFS file exported by DVR 8 163
    tripleUp challenge 7 54
    firstChar challenge 13 57
    wordappend challenge 8 54
    Introduction: Load and Save to file, Document-View interaction inside the SDI. Continuing from the second article about sudoku.   Open the project in visual studio. From the class view select CSudokuDoc and double click to open the header …
    Introduction: Hints for the grid button.  Nested classes, templated collections.  Squash that darned bug! Continuing from the sixth article about sudoku.   Open the project in visual studio. First we will finish with the SUD_SETVALUE messa…
    This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
    This video discusses moving either the default database or any database to a new volume.

    755 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    25 Experts available now in Live!

    Get 1:1 Help Now