CString :: Format的替代? [英] Alternative to CString::Format?

查看:104
本文介绍了CString :: Format的替代?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有更好的替代方法在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 Boost格式 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的替代?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆