Click here to Skip to main content
11,412,833 members (71,823 online)
Click here to Skip to main content

STL split string function

, 13 May 2007 Public Domain
Rate this:
Please Sign up or sign in to vote.
Split a string using a single character delimiter. Template function.

Introduction

Splitting a string using a given delimiter is a very common operation.
Unfortunately the STL doesn't provide any direct way to achieve this.
This split implementation is templatised and uses type deduction to determine what kind of string and container are being passed as arguments, which must must be some form of basic_string<> and any container that implements the push_back() method.

You may either place this code directly into the file where it is used, or create a separate include file for it.

Note that the string type is not fully specified, allowing the traits and allocator to default.
In some situations this may not be what you want and some compilers might not like it.

Background

I wrote this when I found that the code written by Paul J. Weiss with the title STL Split String was more complex than I needed for this common simple case.

The most common case should be the easiest to write.

Source Code

template <typename E, typename C>
size_t split(std::basic_string<E> const& s,
             C &container,
             E const delimiter,
             bool keepBlankFields = true)
{
    size_t n = 0;
    std::basic_string<E>::const_iterator it = s.begin(), end = s.end(), first;
    for (first = it; it != end; ++it)
    {
        // Examine each character and if it matches the delimiter
        if (delimiter == *it)
        {
            if (keepBlankFields || first != it)
            {
                // extract the current field from the string and
                // append the current field to the given container
                container.push_back(std::basic_string<E>(first, it));
                ++n;
                
                // skip the delimiter
                first = it + 1;
            }
            else
            {
                ++first;
            }
        }
    }
    if (keepBlankFields || first != it)
    {
        // extract the last field from the string and
        // append the last field to the given container
        container.push_back(std::basic_string<E>(first, it));
        ++n;
    }
    return n;
}

Example Usage

#include <iostream>
#include <algorithm>
#include <string>
#include <vector>

std::string tests[3] = {
    std::string(""),
    std::string("||three||five|"),
    std::string("|two|three||five|")
};

char* delimiter = "|";

for (int i = 0; i < sizeof(tests)/sizeof(tests[0]); ++i)
{
    std::vector< std::basic_string<char> > x;
    size_t n = split(tests[i], x, delimiter[0], true);
    std::cout << n << "==" << x.size() << " fields" << std::endl;
    if (n)
    {
        std::copy(x.begin(), x.end(),
            std::ostream_iterator<std::string>(std::cout, delimiter));
        std::cout << std::endl;
    }
}

License

This article, along with any associated source code and files, is licensed under A Public Domain dedication

Share

About the Author

David 'dex' Schwartz
Team Leader
Australia Australia
Developing various kinds of software using C/C++ since 1984 or so. Started out writing 8086 asm for direct screen i/o and mouse handling etc.
Used several other languages eg. Java, Python, Clipper/dBase, FORTRAN 77, Natural ADABAS, Unix scripting, etc.
My current work involves Enterprise Content Management on Win32.

Comments and Discussions

 
QuestionQuick split using vector as base class Pin
Dennis Lang at 21-Feb-12 11:25
memberDennis Lang21-Feb-12 11:25 
GeneralCan't compile in GCC 4.40. Pin
scmeiqy at 6-Dec-09 1:15
memberscmeiqy6-Dec-09 1:15 
GeneralRe: Can't compile in GCC 4.40. Pin
David 'dex' Schwartz at 7-Dec-09 22:58
memberDavid 'dex' Schwartz7-Dec-09 22:58 
GeneralRe: Can't compile in GCC 4.40. Pin
David 'dex' Schwartz at 8-Dec-09 1:13
memberDavid 'dex' Schwartz8-Dec-09 1:13 
Generalpush_back Pin
Pablo Aliskevicius at 9-Apr-07 22:17
memberPablo Aliskevicius9-Apr-07 22:17 
AnswerRe: push_back Pin
David 'dex' Schwartz at 12-May-07 5:52
memberDavid 'dex' Schwartz12-May-07 5:52 
Thank you Pablo.
I considered that design but opted for the explicit limitation you described.
The resulting client code gets uglier which is the reason I chose the given version over this one.

Now that someone has suggested it though, I have implemented it.
There are very few changes required.
Change the container type C to OI and rename container to outIt

template <typename E, typename OI>
size_t split(std::basic_string<E> const& s,
OI &outIt,

Then change the line containing push_back to
*outIt++ = std::basic_string<E>(first, it);
in two places and you're done !








Keep it simple
dex

GeneralBoost String Algorithms Pin
Gast128 at 7-Apr-07 22:25
memberGast1287-Apr-07 22:25 
GeneralRe: Boost String Algorithms Pin
Garth J Lancaster at 7-Apr-07 23:05
memberGarth J Lancaster7-Apr-07 23:05 
GeneralRe: Boost String Algorithms Pin
Gast128 at 7-Apr-07 23:54
memberGast1287-Apr-07 23:54 
GeneralRe: Boost String Algorithms Pin
Virtual Coder at 8-Apr-07 10:57
memberVirtual Coder8-Apr-07 10:57 
GeneralRe: Boost String Algorithms Pin
David 'dex' Schwartz at 8-Apr-07 13:49
memberDavid 'dex' Schwartz8-Apr-07 13:49 
GeneralRe: Boost String Algorithms Pin
Gast128 at 8-Apr-07 23:02
memberGast1288-Apr-07 23:02 
GeneralRe: Boost String Algorithms Pin
Stephen Hewitt at 13-May-07 16:18
mvpStephen Hewitt13-May-07 16:18 
GeneralRe: Boost String Algorithms Pin
John M. Drescher at 14-May-07 21:44
memberJohn M. Drescher14-May-07 21:44 
GeneralRe: Boost String Algorithms Pin
Stephen Hewitt at 15-May-07 14:59
mvpStephen Hewitt15-May-07 14:59 
GeneralRe: Boost String Algorithms Pin
John M. Drescher at 15-May-07 15:11
memberJohn M. Drescher15-May-07 15:11 
GeneralRe: Boost String Algorithms Pin
Stephen Hewitt at 15-May-07 15:13
mvpStephen Hewitt15-May-07 15:13 
GeneralRe: Boost String Algorithms Pin
John M. Drescher at 15-May-07 16:47
memberJohn M. Drescher15-May-07 16:47 

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
Web01 | 2.8.150427.1 | Last Updated 13 May 2007
Article Copyright 2007 by David 'dex' Schwartz
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid