I need to subtract text from string with rtf format.
I have a string in rtf format
std::wstring sString= L"{\\rtf1\\ansi\\ansicpg1252\\deff0\\nouicompat\\deflang2057{\\fonttbl{\\f0\\fnil\\fprq12\\fcharset0 Times New Roman;}}\r\n{\\*\\generator Riched20 10.0.19041}\\viewkind4\\uc1 \r\n\\pard\\f0\\fs24 HELLO\\par\r\n}\r\n"
How can I get a string "HELLO" from variable sString?
Since you are on Windows (and assuming you're not looking for a cross-platform solution), there is a way to do this without using any external libraries. What you do is:
Create an invisible
RICHEDITchild window. Since this should be a child window, it does need a valid parent but it doesn't matter much what.Send it an
EM_STREAMINmessage with theSF_RTFflag set to load up your RTF. The data is actually requested from a callback function, so a bit of trickery will be needed there.Send it an
EM_STREAMOUTmessage with theSF_TEXTflag set to retrieve the corresponding plain text. This will call your designated callback function repeatedly until there is nothing more to retrieve, so you'll probably want to append the data to astd::stringor similar each time through.Destroy the window you created at step 1 (or you can keep it kicking around, if you plan to re-use it).
It's a bit fiddly to get it all right, but it's not that difficult. Be prepared to do a bit of background reading.