std::string recplace char

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
 
F

Francesco S. Carta

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?

Hi Simon,
I'm not sure how you would "cast assign" the char into a string, I
would build a temporary string and call the original function in this
way:
-------
replace_inplace(origStr, srchStr, std::string(1, replaceChar));
-------

Since you asked for comments about your code: I don't like the naming
convention, the fact that you're passing a non-const reference and the
doubled return path.

I would write it along the lines of:
-------
void replace(std::string* original,
const std::string& search_this,
const std::string& replace_with) {
const size_t search_len = search_this.size();
if ( original
&& search_len
&& search_this != replace_with ) {
const size_t replace_len = replace_with.size();
std::string::size_type found_at = original->find(search_this);
while (found_at != std::string::npos) {
original->replace(found_at, search_len, replace_with);
found_at = original->find(search_this, found_at +
replace_len );
}
}
}

-------

Warning: code not tested!!!

Matter of tastes, of course, but also matter of already present
conventions. Following an existing project/team convention should have
the precedence over personal preferences.

Hope that helps, just my two cents.

Have good coding,
Francesco
 
J

James Kanze

I wrote a function to do a search and replace in a
std::string, (feel free to comment about it :)).

It should be a true function, and not a "procedure" (a void
function).
// --
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

If both the search and the replace string are single characters,
std::replace will do the job quite well. Otherwise, you're
likely to be shifting a lot of characters around, and it's not
only cleaner, but probably more efficient to use the functional
approach, something like:

std::string
searchAndReplace(
std::string const& source,
std::string const& search,
std::string const& replaceWith )
{
std::string result ;
std::string::const_iterator current = source.begin() ;
std::string::const_iterator next
= std::search( current, source.end(),
search.begin(), search.end() ) ;
while ( next != source.end() ) {
result.append( current, next ) ;
result.append( replaceWith ) ;
current = next + search.size() ;
next = std::search( current, source.end(),
search.begin(), search.end() ) ;
}
result.append( current, source.end() ) ;
return result ;
}

If you really want to do it in place, just assign the results to
source when you're through.
// --
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?

If the search string and the replacement string have different
lengths, you're stuck with either doing a lot of moving around
in the original string (true in place), or copying out of the
original string. A true "in place" replacement is only really
effective if the two have the same length. If that length is 1,
of course, you've just got std::replace; if the length is
greater than 1, then you'll have to write the code yourself.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top