#include <boost/algorithm/string.hpp>
#include <boost/tokenizer.hpp>				//May be needed
using namespace std;
using namespace boost;
using namespace boost::algorithm;



Alter supplied string vs Create a new string from the output

Many of the string functions some in function_name() and function_name_copy() versions, where the _copy() version returns the output and the non _copy() version instead alters the supplied string.

Test A String

Does string contain

	if (contains(my_string, "hello")
		//do something

Get Portions Of A String



Get string up to first

Read each line of a sting

	string text("apple\n\norange\npair");

	typedef boost::tokenizer<boost::char_separator<char> > line_tokenizer;
	line_tokenizer tok(text, boost::char_separator<char>("\n\r"));

	for (line_tokenizer::const_iterator i = tok.begin(), end = tok.end(); i != end ; ++i)		//Get each line of the string, skipping past blank lines
		std::cout << "A:" << *i << std::endl;
Split up a string by character

	string my_text = "07/3/2011";
	vector<string> split_line_elements;
	boost::split(split_line_elements, my_text, boost::is_any_of("/"));
	if (split_line_elements.size() > 0)
		//do something

Remove Bits Of A String

Remove all occurances of a character

	boost::erase_all(my_string, "a");



We benefit hugely from resources on the web so we decided we should try and give back some of our knowledge and resources to the community by opening up many of our company’s internal notes and libraries through mini sites like this. We hope you find the site helpful.
Please feel free to comment if you can add help to this page or point out issues and solutions you have found, but please note that we do not provide support on this site. If you need help with a problem please use one of the many online forums.


Your email address will not be published.