std::string find_first_of and replace
My code below is not behaving as expected. I want to transform
"11/21/13" into "11-21-13"
My loop never exits and the first time through pos = 1, when I would
think it should be 2. Then after the first replace the debugger says my
string is
""1\x1\x1\x1\x1\x1\x1\x1\x1\x1\x1\x1\x1\x1\x1\x1\x1\x1\x1\x1\x1\x1\x1\x1\x1\x1\x1\x1\x1\x1\x1\x1\x1\x1\x1\x1\x1\x1\x1\x1\x1\x1\x1\x1\x1\x1/22/2001"
and I don't know where the heck 1x is coming from.
Any hints?
#include <iostream>
#include <string>
//--------------------------------------------------------------------------------------------------
bool Transform_DatetimeString(const std::string & fromXml, std::string &
toSql)
{
toSql = fromXml;
std::string::size_type pos = std::string::npos;
while( pos = toSql.find_first_of('/') != std::string::npos )
{
toSql = toSql.replace(pos, 1, '-', 1);
}
return true;
}
//--------------------------------------------------------------------------------------------------
int main()
{
const std::string test = "11/21/13";
std::string output;
if( !Transform_DatetimeString(test, output) )
{
return 1;
}
std::cout << output << std::endl;
return 0;
}
--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]