Alternative to CString::Format?

感情迁移 提交于 2020-01-01 14:24:35

问题


Is there any better alternative for doing string formatting in VC6, with syntax checking before substitution?


回答1:


CString offers the Format method for printf-style formatting, but this isn't type-safe.

For type-safe string formatting you could either use std::stringstream / std::wstringstream or the Boost Format library, although these both work with the C++ std::basic_string class template, and not the MFC CString class. I've used both of these successfully in VC6.

Boost Format is nice because it allows you to use printf-like syntax, and will throw an exception if the arguments you supply don't match the format string, whereas string formatting with C++ iostreams tends to make your code quite verbose.

Note that you can create a CString object from a std::string as follows:

std::string s;
CString str( s.c_str() );

I hope this helps!




回答2:


Check out FastFormat. It has an easy syntax, and a "sink" - FastFormat terminology for the thing that receives the result of the formatting operation - for CString.

Something along the lines of:

int i = 1;
std::string ss = "a std string";
CString cs = "a Cstring";

CString result;

fastformat::fmt(result, "i={0}, ss={1}, cs={2}", i, ss, cs);



回答3:


FormatString - smart string formatting
By Ivo Beltchev

Posted on CodeProject



来源:https://stackoverflow.com/questions/404886/alternative-to-cstringformat

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!