Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > how can I replace a substring in a string

Reply
Thread Tools

how can I replace a substring in a string

 
 
silverburgh.meryl@gmail.com
Guest
Posts: n/a
 
      05-30-2007
Hi,

If I have a string like this:
char buff[10];
buff[0] ='h';
buff[1] ='e';
buff[2] ='l';
buff[3] ='l';
buff[4] ='o';

string s(buff);

How can i replace 'll' with 'abc'
and what if I replace 'll' with 'a', will the 'o' move up by 1 index?

Thank you.

 
Reply With Quote
 
 
 
 
Obnoxious User
Guest
Posts: n/a
 
      05-30-2007
http://www.velocityreviews.com/forums/(E-Mail Removed) skrev:
> Hi,
>
> If I have a string like this:
> char buff[10];
> buff[0] ='h';
> buff[1] ='e';
> buff[2] ='l';
> buff[3] ='l';
> buff[4] ='o';
>


buff[5] = 0;

> string s(buff);
>
> How can i replace 'll' with 'abc'
> and what if I replace 'll' with 'a', will the 'o' move up by 1 index?
>


replace(s, "ll", "abc");

void replace(std::string &target, std::string &that, std::string &with) {
std::string::size_type where = target.find(that);
if(where != std::string::npos) {
target.replace(target.begin() + where,
target.begin() + where + that.size(),
with.begin(),
with.end());
}
}

--
OU
 
Reply With Quote
 
 
 
 
Naresh Rautela
Guest
Posts: n/a
 
      05-30-2007
On May 30, 10:04 am, "(E-Mail Removed)"
<(E-Mail Removed)> wrote:
> Hi,
>
> If I have a string like this:
> char buff[10];
> buff[0] ='h';
> buff[1] ='e';
> buff[2] ='l';
> buff[3] ='l';
> buff[4] ='o';
>

You need to insert a null character at postion 5.

> string s(buff);
>
> How can i replace 'll' with 'abc'

s.replace(2,2,"abc");
> and what if I replace 'll' with 'a', will the 'o' move up by 1 index?

yes
>
> Thank you.



 
Reply With Quote
 
Devon Null
Guest
Posts: n/a
 
      05-30-2007
(E-Mail Removed) wrote:
> Hi,
>
> If I have a string like this:
> char buff[10];
> buff[0] ='h';
> buff[1] ='e';
> buff[2] ='l';
> buff[3] ='l';
> buff[4] ='o';
>
> string s(buff);
>
> How can i replace 'll' with 'abc'
> and what if I replace 'll' with 'a', will the 'o' move up by 1 index?
>
> Thank you.
>


Here are all the string class member functions:

http://cppreference.com/cppstring/index.html
 
Reply With Quote
 
Roland Pibinger
Guest
Posts: n/a
 
      05-30-2007
On Wed, 30 May 2007 19:30:52 +0200, Obnoxious User wrote:
[...]
>void replace(std::string &target, std::string &that, std::string &with) {


const correctness?


--
Roland Pibinger
"The best software is simple, elegant, and full of drama" - Grady Booch
 
Reply With Quote
 
Obnoxious User
Guest
Posts: n/a
 
      05-30-2007
Roland Pibinger skrev:
> On Wed, 30 May 2007 19:30:52 +0200, Obnoxious User wrote:
> [...]
>> void replace(std::string &target, std::string &that, std::string &with) {

>
> const correctness?
>
>


void replace(std::string & target,
std::string const & that,
std::string const & with);

--
OU
 
Reply With Quote
 
Robbie Hatley
Guest
Posts: n/a
 
      05-31-2007

<(E-Mail Removed)> wrote in message news:(E-Mail Removed) ups.com...

> Hi,
>
> If I have a string like this:
> char buff[10];
> buff[0] ='h';
> buff[1] ='e';
> buff[2] ='l';
> buff[3] ='l';
> buff[4] ='o';


Triply wrong:
1. Firstly, null-terminated strings need to be, well, null-terminated.
So you'd need to set:
buff[5] = '\0';
2. Why not just initialize the buffer to the string?
char buff[10] = "hello";
3. Why use a char[] buffer at all? Just do this:
std::string MyString ("hello");

> string s(buff);


std::string

> How can i replace 'll' with 'abc'


Like so (tested, working program; compile this for demo):

#include <iostream>
#include <string>
int main (void)
{
std::string S ("hello");
S.replace(S.find("ll"), 2, "abc"); // replace 2 chars at "ll" by "abc"
std::cout << S << std::endl;
return 0;
}

> and what if I replace 'll' with 'a', will the 'o' move up by 1 index?


It decrements by one, from 4 to 3.

> Thank you.


You're welcome.

--
Cheers,
Robbie Hatley
lone wolf aatt well dott com
triple-dubya dott Tustin Free Zone dott org


 
Reply With Quote
 
Robbie Hatley
Guest
Posts: n/a
 
      05-31-2007

"Obnoxious User" <OU@127.0.0.1> wrote in message
news:465db294$0$20045$(E-Mail Removed)...

> replace(s, "ll", "abc");
>
> void replace(std::string &target, std::string &that, std::string &with) {
> std::string::size_type where = target.find(that);
> if(where != std::string::npos) {
> target.replace(target.begin() + where,
> target.begin() + where + that.size(),
> with.begin(),
> with.end());
> }
> }


Ewww. I could replace all of the above with just this:

s.replace(s.find("ll"), 2, "abc");

or more generically:

s.replace(s.find(target), target_size, replacement);

Far more convenient, and far less prone to error. "find" and "replace" are
already built into the std::string class, so might as well use them.

--
Cheers,
Robbie Hatley
lone wolf aatt well dott com
triple-dubya dott Tustin Free Zone dott org


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: How include a large array? Edward A. Falk C Programming 1 04-04-2013 08:07 PM
A simple 'Replace' and 'Substring' question: How to add a string to another string SM Javascript 4 04-27-2007 07:22 AM
how to replace a substring in a string using C? Paul C Programming 19 11-10-2005 08:57 PM
RegEx search for a substring within a substring colinhumber@gmail.com Perl Misc 3 08-03-2005 04:29 PM
Function to replace multiple occurence of substring in a string Dominique Deleris C Programming 4 02-27-2005 10:19 PM



Advertisments