Printing variable in quotation marks C++

  • A+
Category:Languages

given:

string element = "hello"; 

I would like to have the following printed:

"hello" //with quotation marks 

I found how to print words with quotation marks using /"hello/", but I would like to use my variable name element to print out "hello".

 


The closest you can get to have

string element = "hello"; std::cout << element 

and have it print

"hello" 

is to use std::quoted That would make the code

string element = "hello"; std::cout << std::quoted(element); 

and it would output

"hello" 

Do note that std::quoted does more then just add the out quotes. If your string contains quotes then it will modify those and add a / in from of them. That means

string element = "hello /"there/" bob"; std::cout << std::quoted(element); 

will print

"hello /"there/" bob" 

instead of

"hello "there" bob" 

Comment

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: