remove double quotes from a string in c++
Asked Answered
A

7

7

I am stripping off double quotes from a string, but I keep getting this error from the following function. What is the problem here?

void readCSVCell(stringstream& lineStream, string& s) {
    std::getline(lineStream,s,',');
    s.erase(remove( s.begin(), s.end(), '\"' ), s.end());
}

[ERROR]

c.cpp: In function void readCSVCell(std::stringstream&, std::string&):
c.cpp:11: error: cannot convert __gnu_cxx::__normal_iterator<char*, std::basic_string<char, std::char_traits<char>, std::allocator<char> > > to const char* for argument 1 to int remove(const char*)

Aboulia answered 15/4, 2011 at 8:49 Comment(3)
Have you checked this question #556777Billfold
Have you included #include <algorithm>?Ferdinande
Adding 'algorithm' header file and prefixing with std namespace fixed the problem.Aboulia
V
14

Don't you want something like:

s.erase(remove( s.begin(), s.end(), '\"' ),s.end());

As remove returns "A forward iterator pointing to the new end of the sequence, which now includes all the elements with a value other than value" rather than removing the values.

It compiles fine for me though (with gcc 4.4), so perhaps you just need to include <algorithm> and make sure you are either using namespace std or qualify the name.

Vacla answered 15/4, 2011 at 8:52 Comment(1)
It compiles fine for me, so I guess the error is with the namespacing of remove or a conflict with another header file as Space_C0wb0y suggests.Vacla
I
4

Do you have stdio.h included? Then there could be a conflict with remove. This is the reason why you always should prefix std-calls with, well, std::.

Inbreathe answered 15/4, 2011 at 8:52 Comment(0)
V
3

Use std::remove not remove

Violent answered 15/4, 2011 at 8:53 Comment(0)
S
2

You can use below code to remove double quotes from string in C++.

stringVariable.erase(
    std::remove(stringVariable.begin(), stringVariable.end(), '\"'), 
    stringVariable.end());
Strychninism answered 18/2, 2020 at 15:45 Comment(0)
F
1

remove is algorithm, hence you need to do #include <algorithm>. Then while using you should use it as std::remove(...).

Ferdinande answered 15/4, 2011 at 8:55 Comment(0)
E
1

remove requires the algorithm header and is from std namespace

I do find the C++ Reference very helpful for quickly getting usage examples and what headers are required. It may not have complete information for some things but it helps as a good start if I am not sure about how to use some parts of C Library, stream Library, strings library, STL Containers or STL Algorithms

Extinction answered 15/4, 2011 at 9:21 Comment(0)
Y
0

I'm late to the party but here's a way applies to C++14 and above with std::quoted. It's handy since CSV might have custom escape characters as well.

In short, it translates csv double-double quote escaped string "something ""including escaped quotes"" cool" into something "including escaped quotes" cool

Live Demo

#include <iomanip>
#include <iostream>
#include <sstream>
#include <string>
#include <string_view>

int main()
{
    const char delim{'"'};
    const char escape{'"'};
    constexpr static std::string_view TEXT ="\"something \"\"including escaped quotes\"\" cool\"";

    std::stringstream ss;
    ss << TEXT;
    std::cout << "stored as [ "<< ss.str() << "]\n";
    std::string out;
    ss >> std::quoted(out,delim, escape);
    std::cout << "written out ["<< out << "]\n";
    return 0;
}

Output:

stored as ["something ""including escaped quotes"" cool"]
written out [something "including escaped quotes" cool]
Yashmak answered 21/5 at 9:50 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.