# C++ strings

**URL:** <https://boards.straightdope.com/t/c-strings/309819>\
**Category:** Factual Questions\
**Created:** [June 25, 2005, 6:43pm UTC](https://boards.straightdope.com/t/c-strings/309819 "2005-06-25T18:43:21Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![ccwaterback](https://avatars.discourse-cdn.com/v4/letter/c/df705f/32.png) [@ccwaterback](https://boards.straightdope.com/u/ccwaterback)\
**Post date:** [June 25, 2005, 6:43pm UTC](https://boards.straightdope.com/t/c-strings/309819/1 "2005-06-25T18:43:21Z")

</div>

Is there a good reference online that describes all the nice things you can do with C++ strings? Is there a way to do a “split” on a C++ string like you can with Perl?

---

<div class="post-metadata">

**Author:** ![Metacom](https://avatars.discourse-cdn.com/v4/letter/m/ecae2f/32.png) [@Metacom](https://boards.straightdope.com/u/Metacom)\
**Post date:** [June 25, 2005, 7:12pm UTC](https://boards.straightdope.com/t/c-strings/309819/2 "2005-06-25T19:12:29Z")

</div>

> [@ccwaterback](#):
>
> Is there a good reference online that describes all the nice things you can do with C++ strings? Is there a way to do a “split” on a C++ string like you can with Perl?

SGI has the STL documented online [here](http://www.sgi.com/tech/stl/).

Strings are [here](http://www.sgi.com/tech/stl/basic_string.html).

I can’t find any specific “split” method, but it’s easy enough to write one:

```auto

#include <string>
#include <list>
#include <iostream>

using namespace std;
list<string>* split_string(string* str, string::value_type split_on) {
  list<string> *parts = new list<string>();

  string::size_type lpos = 0;
  while(1) {
    string::size_type pos = str->find(split_on,lpos);
    if(pos == str->npos) {
      parts->push_back(str->substr(lpos));
      break;
    } else {
      parts->push_back(str->substr(lpos, pos-lpos));
    }
    lpos = pos + 1;
  }
  return parts;
}
int main(int argc, char* argv[]) {

  string test_string("Oboe,Bassoon,English Horn,Contrabassoon");

  list<string> *parts = split_string(&test_string, ',');

  list<string>::iterator substrings = parts->begin();
  while(substrings != parts->end()) {
    cout << *substrings++ << endl;
  }
}

```

Note: I am **NOT** a C++ expert, I’m just now starting to play with the language after a long break. The above may have subtle bugs of the sort that plague C++ (in particular, I’m unsure if the call to substr returns a heap or stack allocated “string” instance)…

---

<div class="post-metadata">

**Author:** ![ultrafilter](https://avatars.discourse-cdn.com/v4/letter/u/3d9bf3/32.png) [@ultrafilter](https://boards.straightdope.com/u/ultrafilter)\
**Post date:** [June 25, 2005, 7:22pm UTC](https://boards.straightdope.com/t/c-strings/309819/3 "2005-06-25T19:22:28Z")

</div>

Based on **Metacom** ’s code, it looks like strtok is the function you want. The best reference for C++ strings is an experienced programmer.

---

<div class="post-metadata">

**Author:** ![Derleth](https://avatars.discourse-cdn.com/v4/letter/d/b9e5f3/32.png) [@Derleth](https://boards.straightdope.com/u/Derleth)\
**Post date:** [June 26, 2005, 1:01am UTC](https://boards.straightdope.com/t/c-strings/309819/4 "2005-06-26T01:01:30Z")

</div>

> [@ultrafilter](#):
>
> Based on **Metacom** ’s code, it looks like strtok is the function you want. The best reference for C++ strings is an experienced programmer.

strtok is hard to get right, unless C++ has cleaned it up considerably. It keeps an internal state as to where it is in the string, meaning you are forced to split strings one at a time. If you call strtok on one string and then call it on another, the old state is lost.

strtok appears to be part of the cruft C++ picked up from C and shouldn’t be used lightly: It doesn’t operate on members of the string class, it operates on the nul-terminated arrays of char C uses instead of a true string type. If you don’t care about the overhead using the C++ library involves, try the code [here](http://groups-beta.google.com/group/sources/attach/77993fb8841382c8/s2vv-1-0.cpp?part=2) or [here](http://www.24help.info/archive/t-427922-1.html) (search in page for ‘Explode’).

---

<div class="post-metadata">

**Author:** ![Just](https://avatars.discourse-cdn.com/v4/letter/j/4af34b/32.png) [@Just](https://boards.straightdope.com/u/Just)\
**Post date:** [June 26, 2005, 2:38pm UTC](https://boards.straightdope.com/t/c-strings/309819/5 "2005-06-26T14:38:30Z")

</div>

Becaues strtok is so lousy, as **Derleth** described, I’d use the [Boost Tokenizer class.](http://www.boost.org/libs/tokenizer/tokenizer.htm)
