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
ambuliAsked:
Who is Participating?
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.

phoffricCommented:
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
""mbuli
0
ozoCommented:
//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 == '"' ){
      myQuotes.insert(it++,'"');
    }
  }
  std::cout << myQuotes << std::endl;
0
phoffricCommented:
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
"mbuli
0
""mbuli
0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

jkrCommented:
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

0
sarabandeCommented:
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'

or

 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


Sara
1

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
PortletPaulfreelancerCommented:
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
0
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
C++

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.