Inferno Web Development
  • Home
  • Forums
  • About
  • Links
  • Contact

Tutorials List

  • C++ Tutorials

Articles

  • Java
  • PHP
  • Photoshop
  • C#
  • C++ Qt GUI
  • C++ Win32 API
  • C++
  • MASM32
  • General News
  • JavaScript
  • Web Development
  • Windows Tweaks

Popular Tutorials

All time:

  • Website Header with 3D Fold Up / Lift Effect
  • C++ Win32 API Tutorial
  • Perfect C++ String Explode Split
  • Simple C++ DLL Loading a Message Box
  • Simple C++ Pointers and References

Related Articles

  • C/C++ Basic Structures of a Simple Program
  • Simple C++ Pointers and References
  • Inheritance, Polymorphism, and Virtual Functions
  • Advanced Classes, Constructors, Destructors, and Copy Constructors

Programming Tutorials

Home

Perfect C++ String Explode Split

Submitted by Baran Ornarli on Sun, 11/02/2008 - 23:25.
  • C++

Ever wanted the perfect explode or split using the C++ std::string class? Where you can split a string based on a delimiter or separator and store the results in a vector? Here's an easy way to do it!

There were many options available online, but this seemed to be the best way to do it.

void StringExplode(string str, string separator, vector<string>* results){
    int found;
    found = str.find_first_of(separator);
    while(found != string::npos){
        if(found > 0){
            results->push_back(str.substr(0,found));
        }
        str = str.substr(found+1);
        found = str.find_first_of(separator);
    }
    if(str.length() > 0){
        results->push_back(str);
    }
}

Using the std::string function "find_first_of" we get the first instance of separator and store it into found integer.

We then check string::npos and compare with found, so that we make sure our while loop doesn't end until we have reached string::npos (the maximum position of a string index).

If we find an instance, we store it into a std::vector called "results".

Of course we need to update found integer. We also need a solution for the last area, which we push into our results after the while loop.

Here's how to use it:

vector<string> R;
string thisstring = "Inferno Development is a great programming community and tutorial site";
StringExplode(thisstring, " ", &R);
// vector R now contains each word!

Don't forget to include your headers:

#include <vector>
#include <string>

Hopefully this will help many of you with your short small programs dealing with strings. String Explode is a great technique similar to PHP's explode() function and Java's string.split() method.


5
Average: 5 (1 vote)
»
  • Share Tutorial

Post new comment

The content of this field is kept private and will not be shown publicly.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Allowed HTML tags: <a> <em> <strong> <cite> <code> <img> <ul> <ol> <li> <dl> <dt> <dd> <h3> <h4> <h2>
  • Lines and paragraphs break automatically.
  • Image links with 'rel="lightbox"' in the <a> tag will appear in a Lightbox when clicked on.
  • You can enable syntax highlighting of source code with the following tags: <code>, <blockcode>. Beside the tag style "<foo>" it is also possible to use "[foo]".
  • You can use BBCode tags in the text. URLs will automatically be converted to links.
  • Table of contents based on the <h*> tags

More information about formatting options

CAPTCHA
This question is for testing whether you are a human visitor and to prevent automated spam submissions.
   .oooo.    oooooo     oooo  oooooo   oooo  oooooooooo.                .o8       
.dP""Y88b `888. .8' `888. .8' `888' `Y8b "888
]8P' `888. .8' `888. .8' 888 888 .oooooooo 888oooo.
<88b. `888. .8' `888.8' 888oooo888' 888' `88b d88' `88b
`88b. `888.8' `888' 888 `88b 888 888 888 888
o. .88P `888' 888 888 .88P `88bod8P' 888 888
`8bd88P' `8' o888o o888bood8P' `8oooooo. `Y8bod8P'
d" YD
"Y88888P'
Enter the code depicted in ASCII art style.

Navigation

  • Home
  • Forums
  • Image Gallery
  • Links
  • About
  • Contact

Why Register? Contribute articles and tutorials to ID, earn titles, learn from ID programming projects, and advertise your blog in our forums.

  • Forum Community
  • Register Now
  • Write an Article


Copyright © Inferno Development 2008. All Rights Reserved.