S
Simon
Hi,
I wrote a function to do a search and replace in a std::string, (feel
free to comment about it ).
// --
void replace_inplace
(
std::string& origStr,
const std::string& srchStr,
const std::string& replaceStr
)
{
const size_t len = replaceStr.length();
const size_t lens = srchStr.length();
if( 0 == lens || (len == lens && replaceStr == srchStr) )
{
return; // what are we looking for?
}
std::string::size_type loc = origStr.find(srchStr);
while (loc != std::string::npos)
{
origStr.replace(loc, lens, replaceStr);
loc = origStr.find(srchStr, loc + len );
}
}
// --
But I am looking for a similar function that would replace a search
string with a single char
// --
void replace_inplace
(
std::string& origStr,
const std::string& srchStr,
const char replaceChar
)
{
...
}
Of course I could cast assign the char into a std::string and call the
first function, but I was wondering if there was a more efficient
solution to replace a std::string with a char?
Many thanks
Simon
I wrote a function to do a search and replace in a std::string, (feel
free to comment about it ).
// --
void replace_inplace
(
std::string& origStr,
const std::string& srchStr,
const std::string& replaceStr
)
{
const size_t len = replaceStr.length();
const size_t lens = srchStr.length();
if( 0 == lens || (len == lens && replaceStr == srchStr) )
{
return; // what are we looking for?
}
std::string::size_type loc = origStr.find(srchStr);
while (loc != std::string::npos)
{
origStr.replace(loc, lens, replaceStr);
loc = origStr.find(srchStr, loc + len );
}
}
// --
But I am looking for a similar function that would replace a search
string with a single char
// --
void replace_inplace
(
std::string& origStr,
const std::string& srchStr,
const char replaceChar
)
{
...
}
Of course I could cast assign the char into a std::string and call the
first function, but I was wondering if there was a more efficient
solution to replace a std::string with a char?
Many thanks
Simon