-2

Possible Duplicate:
How to find and replace string?

How I can get function like:

string gg="13332";
gg.replace("333","");//gg="12"

? I can't find anything at standart library. EDIT: my function:

     bool isDalshe=false;
         do{
             isDalshe=false;
         for(int i=0;i<10;i++){
             for(int j=3;j<9;j++){
                 found=sk.find(mas[i][j].ch);
                 if(found!= std::string::npos){
                 glob_c+=mas[i][j].i;
                 cout<<"Found: "<<int(found)<<endl;
//In this place I need to replace "mas[i][j].ch" with "" in sk
                 isDalshe=true;
                 }
             }}
         }
         while(isDalshe);
Community
  • 1
  • 1
SevenDays
  • 3,718
  • 10
  • 44
  • 71

4 Answers4

3

You need to use a mix of replace and find.

http://www.cplusplus.com/reference/string/string/find/
http://www.cplusplus.com/reference/string/string/replace/

Tyler Ferraro
  • 3,753
  • 1
  • 21
  • 28
2

std::string has a replace method that's close to what you're looking for.

This should do it, I think:

std::string gg="13332";
gg.replace(1, 3, "");//gg="12"

But better, use Boost instead: Here. Boos can do the find and replace in a single call, including multiple replacements, something that with the std::string methods you'd have to do manually.

littleadv
  • 20,100
  • 2
  • 36
  • 50
2

All of the forms of replace in std::string take positions, so you have to find the existing string, and replace it with the new one as two separate steps, something like this:

while ((pos=gg.find("333")) != std::string::npos)
    gg.replace(pos, 3, "");

Another option is to treat your string as a collection, and use std::replace on it. This, however, is really intended to work on an element-for-element replacement, so for your task it won't work very well. If you really want to just remove all the a's from the string, you could use std::remove, but that won't work if (for example) you wanted to turn "133323" into "123" (it would remove all the 3s, with no way to tell it you only want to delete a string of three '3's in a row.

Jerry Coffin
  • 476,176
  • 80
  • 629
  • 1,111
  • Thnaks! That's exactly what I wanted. – SevenDays Oct 31 '11 at 22:11
  • I think providing ready solutions for people, especially those participating in competitions and contests, is a very bad idea. We'll have to work with them when they grow up, and I'd like them to learn how to learn before they come here and ruin projects with stupid bugs because they're too lazy to look things up. – littleadv Oct 31 '11 at 22:13
  • For the first I'm not a member of any sport programming competition, for the second I didn't find anything about this problem in the internet, because I don't know length of the string that needs to be replaced. – SevenDays Nov 01 '11 at 21:41
1

Maybe this helps: Segmentation error while replacing substrings of a string with other substring

[since there is a C - tag under the post]

Community
  • 1
  • 1
wildplasser
  • 43,142
  • 8
  • 66
  • 109