removing chars from string

what was the wrong here for removing all occurence of chars [,",]


1
2
3
4
5
6
7
8
9

        string str ("["abc","ces","el","bee"]");
        char chars[]= "[""]" ;
        for(unsigned int i=0; i<strlen(chars);++i)
        {
                str.erase(remove(str.begin(),str.end(),chars[i]),str.end());
        }
        cout<<str<<endl;

final output will be : abc,ces,el,bee

help me to figure out .
Last edited on
If you want a string to contain quote characters you will have to escape them by putting a backward slash in front of them, otherwise they will mark the end of the string.
 
"[\"abc\",\"ces\",\"el\",\"bee\"]"


Another option is to use raw string literals.
 
R"(["abc","ces","el","bee"])"

Last edited on
closed account (48T7M4Gy)
As a first step what yo need to do is:
a) input string is ["abc","ces","el","bee"] i.e. the first and last " characters are not part of the string to be modified

b) the expected output is: ???

c) the actual output from your is: ???

Tell us that to make it easier to see what your problem is.
Thanks, but i know that \ before ".

I m getting the string like ["abc","ces","el","bee"] format .i want to make this to normal coma separated.

square brackets are included in string by default i want to remove that.
Last edited on
If you read the string from the user or from a file you probably need to do nothing with the string, but you still need to escape the other string literal that you have on the line below.
That was a string response from magic suggest box changes. i use to eliminate the string literals for further

any idea how can i erase the literals and make into coma separated .
if i remove all literals then it will be prob where can i insert the coma in between
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include <iostream>
#include <string>

int main()
{
    const std::string original_str = R"(["abc","ces","el","bee"])" ;
    std::cout << "original: " << original_str << "\n\n" ;

    const std::string unwanted = "\"[]" ;
    std::cout << "unwanted: " << unwanted << "\n\n" ;

    std::string sanitised_str  ;
    for( char c : original_str ) if( unwanted.find(c) == std::string::npos ) sanitised_str += c ;
    std::cout << "sanitised: " << sanitised_str << '\n' ;
}

http://coliru.stacked-crooked.com/a/3e364fe07e89b1a5
closed account (48T7M4Gy)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include <iostream>
#include <string.h>

int main()
{
    char str[] = (R"(["abc","ces","el","bee"])");
    std::cout << str << std::endl;
    
    char *pch;
    
    pch = strtok (str, "\"[],");
    while (pch != NULL)
    {
        std::cout << pch << std::endl;
        pch = strtok (NULL, "\"[],");
    }
    
    return 0;
}
Last edited on
Hey thanks guys, but no need of removing the literals i think so

i just remove the first and last char of string and then my old logic ..:)

1
2
3
4
5
str.erase(0,1);
str.erase(magic_filename.size()-1,1);

str.erase(std::remove(str.begin(), str.end(), '"'), str.end());	

my desired output with coma separated ..:)
Topic archived. No new replies allowed.