Avinash
Avinash

Reputation: 13257

Double Quoted Strings in C++

How to converted string with space in double quoted string. For Example: I get string

c:\program files\abc.bat

I want to convert this string to "c:\program files\abc.bat" but only if there is space in the string.

Upvotes: 3

Views: 653

Answers (3)

Drew Hall
Drew Hall

Reputation: 29047

std::string str = get_your_input_somehow();

if (str.find(" ") != std::string::npos) {
  str = "\"" + str + "\"";
}

Upvotes: 0

Ton van den Heuvel
Ton van den Heuvel

Reputation: 10528

Assuming the STL string s contains the string you want to check for a space:

if (s.find(' ') != std::string::npos)
{
  s = '"' + s + '"';
}

Upvotes: 5

Simon Linder
Simon Linder

Reputation: 3428

Search for white spaces. If found add \" to the front and the end of the string. That would be an escaped quotation mark.

Upvotes: 2

Related Questions