问题描述
有没有更好的替代方法在VC6中进行字符串格式化,在替换前进行语法检查?
Is there any better alternative for doing string formatting in VC6, with syntax checking before substitution?
推荐答案
CString
为 printf
格式化提供格式
方法,
CString
offers the Format
method for printf
-style formatting, but this isn't type-safe.
对于类型安全的字符串格式化,您可以使用 std :: stringstream
/ std :: wstringstream
或 library,虽然这两个都使用C ++ std :: basic_string
类模板,而不是MFC CString
类。我在VC6中成功使用了这两个。
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格式很好,因为它允许你使用 printf
类似的语法,如果你提供的参数与格式字符串不匹配,会抛出异常,而使用C ++ iostreams的字符串格式化往往会使你的代码非常冗长。
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.
注意,您可以从 std :: string
中创建 CString
对象,如下所示:
Note that you can create a CString
object from a std::string
as follows:
std::string s;
CString str( s.c_str() );
我希望这有助于!
这篇关于CString :: Format的替代?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!