WinapiZone.net




Tutorials > WinAPI > Functions > explode

explode

explode is the C++ version of the PHP explode function, that splits a string into pieces delimited by a separator. Returns a C++ vector of the pieces.
typedef std::basic_string<TCHAR> tstring; std::vector<tstring> explode(tstring stringToExplode, const tstring& separator) { std::vector<tstring> explodedStrings; int sepPos = 0; while( (sepPos = stringToExplode.find(separator, 0)) != tstring::npos ) { explodedStrings.push_back(stringToExplode.substr(0, sepPos)); stringToExplode.erase(0, sepPos + separator.size()); } if(stringToExplode != _T("") ) explodedStrings.push_back(stringToExplode); return explodedStrings; }

Arguments

stringToExplode
Literally, the string to be exploded.
separator
The string that separates the pieces of the "stringToExplode"

Example

tstring string = _T("hello,world,bye");
std::vector<tstring> pieces = explode(string, _T(","));

//pieces[0] = "hello"
//pieces[2] = "world"
//pieces[3] = "bye"'

Comments

No comments yet.

Your comment:

Name:
E-mail or homepage:
Captcha:
captcha

Script by Alex



This page was last modified 34 months, 3 weeks, 2 days and 9 hours ago