1

I am having problem formatting a string which contains quotationmarks.

For example, I got this std::string: server/register?json={"id"="monkey"}

This string needs to have the four quotation marks replaced by \", because it will be used as a c_str() for another function.

How does one do this the best way on this string?

{"id"="monkey"}

EDIT: I need a solution which uses STL libraries only, preferably only with String.h. I have confirmed I need to replace " with \".

EDIT2: Nvm, found the bug in the framework

Cœur
  • 37,241
  • 25
  • 195
  • 267
KaiserJohaan
  • 9,028
  • 20
  • 112
  • 199
  • What do you mean by "quotation marks replaced by `\"`"? Is the string sample a literal in your code or a parameter taken from somewhere during runtime? – Griwes Sep 27 '11 at 13:18
  • 1
    Are you putting this into the source code of the program as a string literal? That's the only time you need to escape the quotes. In a typical case like reading the data from a file or network connection, you don't need (or want) to escape the quotes. – Jerry Coffin Sep 27 '11 at 13:19
  • In c++ and C# '\"' donates quotation mark... – vinay Sep 27 '11 at 13:23

2 Answers2

5

it is perfectly legal to have the '"' char in a C-string. So the short answer is that you need to do nothing. Escaping the quotes is only required when typing in the source code

std::string str("server/register?json={\"id\"=\"monkey\"}")
my_c_function(str.c_str());// Nothing to do here

However, in general if you want to replace a substring by an other, use boost string algorithms.

#include <boost/algorithm/string/replace.hpp>
#include <iostream>
int main(int, char**)
{
    std::string str = "Hello world";
    boost::algorithm::replace_all(str, "o", "a"); //modifies str
    std::string str2 = boost::algorithm::replace_all_copy(str, "ll", "xy"); //doesn't modify str
    std::cout << str << " - " << str2 << std::endl;
}
// Displays : Hella warld - Hexya warld
Tristram Gräbener
  • 9,601
  • 3
  • 34
  • 50
  • I am positive my application dosn't work without \" in the C-string. It is being sent down to a really large framework for processing so I can't change what happens. Anyway, do you have another example of just using the STL libs and not boost to replace " with \" ? Thanks! – KaiserJohaan Sep 27 '11 at 14:22
  • All right, so the framework you're working on has a bug. If you can't use boost (I really pitty you... developping in C++ without boost is like cycling without pedals). You'll have to write your own function based on std::string::find that will return the position to a quote, and then use std::string::replace http://www.cplusplus.com/reference/string/string/replace/ – Tristram Gräbener Sep 27 '11 at 14:53
1

If you std::string contains server/register?json={"id"="monkey"}, there's no need to replace anything, as it will already be correctly formatted.

The only place you would need this is if you hard-coded the string and assigned it manually. But then, you can just replace the quotes manually.

Luchian Grigore
  • 253,575
  • 64
  • 457
  • 625