Sam said:
Hi
I've seen that the string classes in the .net framework have a cool (new?)
function: splitting strings and returning the substrings in an array. Is
there any equivalent to this in "old-style" MFC C++?
If by "old-style" you mean "standard", you might do something like this:
#include <vector>
#include <string>
#include <iostream>
void split_string(const std::string& source, const std::string& delims,
std::vector<std::string>& result)
{
std::string::size_type begin(0);
std::string::size_type end(0);
while (end != std::string::npos)
{
end = source.find_first_of(delims, begin);
result.push_back(source.substr(begin, end - begin));
begin = end + 1;
}
}
int main()
{
std::vector<std::string> strings;
split_string("This |is^ a string with;[ several ]delimiters.", ";[]^|",
strings);
for (std::vector<std::string>::size_type i = 0; i < strings.size(); ++i)
{
std::cout << '"' << strings
<< '"' << std::endl;
}
}
Tested with g++ 3.2: VC++ is on my other machine - sorry. Can't speak for
CString or whatever else might be lurking in the MFC. Apologies if I
overlooked some feature of the STL that already does this .
Arnold the Aardvark
http://www.codeproject.com/cpp/garbage_collect.asp