How to save " in a string in C++? -


so have following code doesn't work. couldn't figure out how it.

 std::string str("q850?51'18.23""); 

first problem face " (quotation mark). cannot save string because @ end of string have 2 " characters , c++ doesn't let me save whole string.

second want split string , save in different variables.

e.g.;

double = 850; double j = 51; double k = 18.23; 

you need escape quotation mark require in string;

std::string str("q850?51'18.23\""); //                            ^ escape quote here 

the cppreference site has list of these escape sequences.

alternatively use raw string literal;

std::string str = r"(q850?51'18.23")"; 

the second part of problem dependent on format , predictability of data;

  • if fixed width, simple index , used extract numbers , convert double require.
  • if delimited characters above, can consume string each of delimiters extracting numbers in-between them (you should able find suitable libraries assist this).
  • if further unknown composition, may limited consuming string 1 character @ time , extracting numerical values between non-numerical values.

Comments