How to replace a double quote with two double quotes in C++

Hi there,
If I have a string like a"mbuli and wanted to get a""mbuli.  How can I do this?

Thank you
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Is this what you had in mind?
#include <iostream>
#include <string>

int main()
  std::string myQuotes = "\"\"mbuli";
  std::cout << myQuotes << std::endl;

Open in new window

$ ./a.exe
//or did you had in mind something like this?
 std::string myQuotes = "a\"mbuli";
  std::cout << myQuotes << std::endl;
  for( std::string::iterator it=myQuotes.begin(); it!=myQuotes.end(); ++it ){
    if( *it == '"' ){
  std::cout << myQuotes << std::endl;
Saw that you want replacement. Here's one way:
#include <iostream>
#include <string>

int main()
  std::string myQuotes = "\"mbuli";
  std::string quoteChar = "\"";
  std::cout << myQuotes << std::endl;

  size_t posQuote = myQuotes.find(quoteChar);
  std::cout << posQuote << std::endl;
  myQuotes.replace( posQuote, 1, "\"\"");
  std::cout << myQuotes << std::endl;

Open in new window

$ ./a.exe
IT Pros Agree: AI and Machine Learning Key

We’d all like to think our company’s data is well protected, but when you ask IT professionals they admit the data probably is not as safe as it could be.

I rathewr assume you want to use that for string variables, then it'd be more like

#include <iostream>
#include <string>
using namespace std;

string double_double_quotes(const string& strIn) {

  unsigned int len = strIn.length();
  string strOut;

  for (unsigned int i = 0; i < len; ++i) {

    if (strIn[i] == '\"') strOut += "\"\"";
     else strOut += strIn[i]; 

  return strOut;

int main() {

  string test = "a\"mbuli";

  cout << double_double_quotes(test) << endl;

  return 0;

Open in new window

it seems from your previous question 'SQL handling single and double quotes' that you need to replace single quotes which are included in a single quoted sql string value and double quotes which are included in a double quoted sql string value.

you were told that you could use double quote characters within single-quoted strings and vice versa without further escaping. you also were told that you could/should use single quotes for sql string values since double-quoted strings have a different meaning for some sql dialects.

so your second sample statement

DELETE FROM Table WHERE Name= "nameString";

actually should be (using single quotes)

DELETE FROM Table WHERE Name= 'nameString';

if so, you wouldn't need to do any correction if the literal contained a double quote character:

DELETE FROM Table WHERE Name= 'nameStringWith"Included';

however, if the literal contains a single quote character like

DELETE FROM Table WHERE Name= 'name'String' and Country='some'other';

you got a problem since the expression right of Name= could ambiguously parsed either as



 Name='name''String'' and Country=''some''other'

though the first one is more likely you badly could decide programmatically, since you can't easily find out the end of a quoted string if you have multiple single quotes in a statement where some belong to the sql syntax and some are within values.

to come out from this, you should not replace quote characters in a sql statement but in string variables. so if you would have

std::string sql = "DELETE FROM Table WHERE Name= '%name%';"
std::string name = "a'mbuli";

it is rather simple to do like

int pos, lpos = 0;
while ((pos = (int)name.find('\'', lpos)) != std::string::npos)
      name.insert(pos, '\'');
      lpos = pos+2;
pos = (int)sql.find("%name%");
if (pos != std::string::npos)
       sql.replace(pos, 6, name);

Open in new window


Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
PortletPaulEE Topic AdvisorCommented:
Please refer to your previous question on SQL handling of quotation marks.

In particular:

ID: 41034047 by Qlemo
Always use only single quotes to enclose a string literal. Double quotes have a different meaning in ANSI SQL. Inside of single quotes, you only need to care about literal single quotes (which have to be doubled), everything else including double quotes just works.
and also note that MySQL has some product specific information on quoted literals.

ID: 41036892 by myself
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.