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

3
  • 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? Commented Sep 27, 2011 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. Commented Sep 27, 2011 at 13:19
  • In c++ and C# '\"' donates quotation mark... Commented Sep 27, 2011 at 13:23

2 Answers 2

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
Sign up to request clarification or add additional context in comments.

2 Comments

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!
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 cplusplus.com/reference/string/string/replace
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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.