Click here to Skip to main content
11,410,333 members (60,791 online)
Click here to Skip to main content

String Parsing Class (supports quoted strings)

, 14 Mar 2002
Rate this:
Please Sign up or sign in to vote.
Parse strings with specified delimiter and specified quote character
<!-- Link to source file download -->
  • Download source files - 102 Kb
  • <!-- Add the rest of your HTML here -->


    How many times have you wanted to parse a string and had to re-write a little function here or there to extract what you want? This class is exactly what you've been waiting for.

    How To Use CQStringParser

    Add the following include:

    #include "QStringParser.h"
    And where it's needed, do something like this (I used this code to test the class):
        CString sTest = "abc,def,\"efg,hij\",klm,nop,\"qrstuv\",wxyz";
        CQStringParser p(sTest, ',', '\"');
        CString sBuffer = "";
        int nCount = p.GetCount();
        if (nCount > 0)
            for (int i = 1; i <= nCount; i++)
                sBuffer += (p.GetField(i) + CString("\n"));
    	CString sTemp;
    	int nElement;
    	sTemp = p.Find("efg", &nElement);
    	if (nElement > 0)
    	    sBuffer.Format("Found string - %s", sTemp);
    	    AfxMessageBox("No matching string found ('efg').");
    	sTemp = p.FindExact("abc",&nElement);
    	if (nElement > 0)
    	    sBuffer.Format("Found string - %s", sTemp);
    	    AfxMessageBox("No exactly matching string found ('efg').");
            AfxMessageBox("No strings parsed.");

    You can (of course) easily create an array of CQStringParser objects if needed, and read in an entire delimited file before processing the parsed strings. Alternatively, you can use the same object over and over again with different strings.

    The parsed fields begin at element #1 because I store the original string in element 0.

    How To Use CQStdStringParser

    Add the following include:

    #include "QStdStringParser.h"

    And where it's needed, do something like this (I used this code to test the class):

        std::string sTest = "abc,def,\"efg,hij\",klm,nop,\"qrstuv\",wxyz";
        CQStdStringParser p(sTest, ',', '\"');

    The strings being passed into the class and retrieved from the class are of type std:string, but other than that, the class functions identically to its MFC-specific cousin (which works with CStrings).


    06 May 2001 - As I use my classes they mature and grow, and CQStringParser is no exception. In this iteration, I've simplified the code by eliminating the overloaded constructor and parsing functions. I've also added new functionality. You can now Add, Set (change), Insert, and Delete fields from the parsed string. The demo (link at top this article) includes a simple dialog-based application which allows you to play around with the primary functionality of the class. As usual, the class is fully documented. Have a ball.

    10 August 2001 - I recently had reason to need the use of this class in a NON-MFC evnironment, and in order to facilitate this requirement, I created a new version of the class that uses STL instead of the MFC collection classes. The demo program now contains both the CQStringParser class, as well as the CQStdStringParser class. The only externally obvious difference is that the strings you pass in and get back are of type std::string instead of CString. The method names and class functionality are otherwise identical to the original class.

    15 March 2002 - Fixed a parsing bug in the string parser classes, and changed the sample app to allow you to change the quote and/or delimiter character in the dialog box.


    This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

    A list of licenses authors might use can be found here


    About the Author

    John Simmons / outlaw programmer
    Software Developer (Senior)
    United States United States
    I've been paid as a programmer since 1982 with experience in Pascal, and C++ (both self-taught), and began writing Windows programs in 1991 using Visual C++ and MFC. In the 2nd half of 2007, I started writing C# Windows Forms and ASP.Net applications, and have since done WPF, Silverlight, WCF, web services, and Windows services.

    My weakest point is that my moments of clarity are too brief to hold a meaningful conversation that requires more than 30 seconds to complete. Thankfully, grunts of agreement are all that is required to conduct most discussions without committing to any particular belief system.

    Comments and Discussions

    Generalparsing wrong Pinmemberohh5-Aug-10 2:44 
    GeneralBelated Thanks! Pinmemberbaloneyman7-Feb-09 15:27 
    Questionletterlike sysbol? Pinmembermanh_duc1-Mar-07 14:45 
    AnswerRe: letterlike sysbol? PinmvpJohn Simmons / outlaw programmer2-Mar-07 0:08 
    GeneralAddField has a problem with empty original string PinmemberHans-Georg Ulrich12-Aug-04 0:19 
    To build token strings from scratch using AddField, I patched as follows:
    // --------  ----  -------------------------------------------------------------
    // 08/12/04  HGU   allow empty original string
    // 02/25/01  jms   Added function.
    void CQStringParser::AddField(CString sText)
    	CString original;
    	if (GetOriginalString().IsEmpty())
    		original = sText;
    		original = GetOriginalString() + CString(m_cDelimiter) + sText;
    	ResetOriginalString(original, m_cDelimiter, m_cQuoter);
    Useful class, thank you !
    GeneralJapanese text parsing Pinmemberr_senthil1924-Jul-03 15:46 
    Generalbug fix PinmemberEdwin Geng20-Jul-03 1:27 
    QuestionNew Bug ?? PinmemberTutu19-Sep-02 22:23 
    AnswerRe: New Bug ?? PinmemberJohn Simmons / outlaw programmer22-Nov-06 5:00 
    GeneralFound some bugs PinsussL. Kohnert9-Jul-02 4:13 
    GeneralRe: Found some bugs PinmemberJohn Simmons / outlaw programmer10-Jul-02 5:01 
    GeneralVery useful thanks PinmemberJ Cardinal20-Jun-02 9:50 
    GeneralRe: Very useful thanks PinmemberJohn Simmons / outlaw programmer22-Jun-02 3:35 
    GeneralNice, but... Pinmembersupertedusa30-May-02 11:34 
    GeneralRe: Nice, but... PinmemberJohn Simmons / outlaw programmer30-May-02 14:22 
    GeneralRe: Nice, but... Pinmembersupertedusa30-May-02 15:06 
    GeneralRe: Nice, but... PinmvpJohn Simmons / outlaw programmer31-Jul-07 6:09 
    GeneralWow - a Linux Guy PinmemberJohn Simmons / outlaw programmer15-Mar-02 4:52 
    GeneralBugs present PinmemberAnonymous11-Mar-02 4:20 
    GeneralRe: Bugs present PinmemberJohn Simmons / outlaw programmer15-Mar-02 3:48 
    GeneralExcellent PinmemberThomas George27-Oct-01 8:56 
    GeneralRe: Excellent PinmemberJohn Simmons / outlaw programmer28-Nov-01 8:03 
    GeneralNeed help please PinmemberAnonymous3-Sep-01 16:16 
    GeneralRe: Need help please PinmemberJohn Simmons / outlaw programmer14-Oct-01 5:46 
    QuestionHow delimiter is also string (e.g.. @$)?? PinmemberAnonymous27-Aug-01 14:07 
    AnswerRe: How delimiter is also string (e.g.. @$)?? PinmemberJohn Simmons / outlaw programmer14-Oct-01 5:45 
    GeneralRe: How delimiter is also string (e.g.. @$)?? PinmemberAnonymous15-Oct-01 4:45 
    GeneralRe: How delimiter is also string (e.g.. @$)?? PinmemberJohn Simmons / outlaw programmer15-Oct-01 4:47 
    QuestionAnyone Interested in non-MFC version? PinmemberJohn Simmons / outlaw programmer7-Aug-01 10:24 
    AnswerRe: Anyone Interested in non-MFC version? PinmemberAnonymous10-Aug-01 20:56 
    GeneralRe: Anyone Interested in non-MFC version? PinmemberJohn Simmons / outlaw programmer11-Aug-01 1:58 
    GeneralFound a problem when parsing empty strings PinmemberJohn Simmons / outlaw programmer21-May-01 4:28 
    GeneralParse with quotes Pinmemberhearties18-May-01 0:23 
    GeneralRe: Parse with quotes PinmemberJohn Simmons / outlaw programmer18-May-01 1:24 
    GeneralRe: Parse with quotes - Debug Assertion Failed" PinmemberAnonymous14-Aug-01 9:53 
    GeneralAdded Functionality PinmemberJohn Simmons / outlaw programmer6-May-01 7:05 
    GeneralBug in ParseStringWithQuoter() PinmemberPascal Dimassimo1-May-01 7:02 
    GeneralThe 'all in one' solution PinmemberRemon14-Apr-01 5:51 
    GeneralProblem with quoted strings PinmemberHelge Opgard6-Mar-01 23:09 
    GeneralRe: Problem with quoted strings PinmemberHelge Opgard7-Mar-01 0:31 
    GeneralRe: Problem with quoted strings PinmemberJohn Simmons / outlaw programmer15-Mar-01 7:26 
    GeneralRe: Problem with quoted strings PinmemberJohn Simmons / outlaw programmer9-May-01 8:10 
    GeneralOverkill - what;s wrong with strtok. PinmemberAnonymous20-Jan-01 5:51 
    GeneralRe: Overkill - what;s wrong with strtok. PinmemberJohn Simmons / outlaw programmer20-Jan-01 8:15 
    GeneralRe: Overkill - what;s wrong with strtok. PinmemberGennady Oster22-Jan-01 3:39 
    GeneralRe: Overkill - what;s wrong with strtok. PinmemberD.D. de Kerf6-May-01 21:37 
    GeneralRe: Overkill - what;s wrong with strtok. Pinmemberanonymous13-Aug-01 4:58 
    GeneralRe: Overkill - what;s wrong with strtok. PinmemberTim Smith15-Mar-02 4:32 

    General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

    Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

    | Advertise | Privacy | Terms of Use | Mobile
    Web02 | 2.8.150414.5 | Last Updated 15 Mar 2002
    Article Copyright 2001 by John Simmons / outlaw programmer
    Everything else Copyright © CodeProject, 1999-2015
    Layout: fixed | fluid