在C++中实现类似C#的字符串格式功能
2010-03-18 17:44
621 查看
在C#中,我们可以这样格式化一个字符串
String s = String.Format ("The {0} costs {1}{2}", "car", 30000, "yuan");
而在C++中,我们只能这样处理
sprintf (s, "The %s costs %d%s", "car", 30000, "yuan");
这种处理方法极不安全
这里实现了一个类似C#的字符串格式化函数,可以像下面这样格式化字符串了
std::string s = FormatString ("The $0 costs $1$2", "car", 30000, "yuan");
这里使用'$'作为格式化前缀
$0 输出第一个参数
$1 输出第二个参数
......
$$ 输出 '$'
$c 输出 '$c'
函数代码:
相关测试函数
String s = String.Format ("The {0} costs {1}{2}", "car", 30000, "yuan");
而在C++中,我们只能这样处理
sprintf (s, "The %s costs %d%s", "car", 30000, "yuan");
这种处理方法极不安全
这里实现了一个类似C#的字符串格式化函数,可以像下面这样格式化字符串了
std::string s = FormatString ("The $0 costs $1$2", "car", 30000, "yuan");
这里使用'$'作为格式化前缀
$0 输出第一个参数
$1 输出第二个参数
......
$$ 输出 '$'
$c 输出 '$c'
函数代码:
template <class T0, class T1, class T2, class T3, class T4> std::string FormatString (const std::string &msg, int paraSize, const T0 &t0, const T1 &t1, const T2 &t2, const T3 &t3, const T4 &t4) { if (paraSize > 5) paraSize = 5; const char PREFEX = '$'; std::stringstream ss; for (std::string::size_type i = 0; i < msg.size (); ++i) { if (msg[i] != PREFEX) // not '$' { ss << msg[i]; } else if (i == msg.size () - 1) // '$' is the end of msg { ss << PREFEX; } else // is '$' { i++; char c = msg[i]; // get next character int index = c - '0'; if (c == PREFEX) // next character is '$' { ss << PREFEX; } else if (index < 0 || index >= paraSize) // next character is not valid para ('0' - '5') { ss << PREFEX << c; } else // valid para "$0", "$1", ... { switch (index) { case 0: ss << t0; break; case 1: ss << t1; break; case 2: ss << t2; break; case 3: ss << t3; break; case 4: ss << t4; break; default: assert (false); break; } } } } return ss.str (); } template <class T0, class T1, class T2, class T3> std::string FormatString (const std::string &msg, const T0 &t0, const T1 &t1, const T2 &t2, const T3 &t3) { return FormatString (msg, 4, t0, t1, t2, t3, 0); } template <class T0, class T1, class T2> std::string FormatString (const std::string &msg, const T0 &t0, const T1 &t1, const T2 &t2) { return FormatString (msg, 3, t0, t1, t2, 0, 0); } template <class T0, class T1> std::string FormatString (const std::string &msg, const T0 &t0, const T1 &t1) { return FormatString (msg, 2, t0, t1, 0, 0, 0); } template <class T0> std::string FormatString (const std::string &msg, const T0 &t0) { return FormatString (msg, 1, t0, 0, 0, 0, 0); } std::string FormatString (const std::string &msg) { return FormatString (msg, 0, 0, 0, 0, 0, 0); }
相关测试函数
void FormatString_test () { std::cout << FormatString ("$hahaha$", 1, 2) << std::endl; std::cout << FormatString ("haha $0 sss", 3) << std::endl; std::cout << FormatString ("haha $0 sss $1 ddd $3$5", 3) << std::endl; std::cout << FormatString ("haha $$0 s$ss $1 ddd $3$5", 3, "dddd") << std::endl; std::cout << FormatString ("haha $$$0 sss $1 ddd $3$5", 'a', "dddddd", 3, 0.3333) << std::endl; }
相关文章推荐
- c#控件实现类似c++中ocx控件功能,Event事件
- c++ 连接两个字符串,实现类似strcat 功能
- c++ 连接两个字符串实现代码 实现类似strcat功能(转)
- C#字符串格式化的C++实现
- 记录一下C++和C#中的字符串处理与编码格式的问题
- Delphi中实现C++/Java类似静态变量的功能
- C# 自定义格式字符串随笔(IFormattable,IFormatProvider,ICustomFormatter三接口的实现)
- 在C++实现C#中的属性(Property)功能的尝试: MACRO篇(1)
- Delphi中实现C++/Java类似静态变量的功能
- C++:模拟实现类似<time.h>的计时功能
- 利用C++重载实现类似“类成员指针”的功能
- C++ 实现类似Notepad++ Ctrl+F 的搜索功能
- C++实现判断一个字符串是否为UTF8或GBK格式的方法
- 在C#中实现类似C++函数指针数组的功能
- C++实现round四舍五入四位小数,类似C#的round函数
- C#+AE实现类似IDentify功能及对高亮显示相关接口的总结
- C# DataGridView中实现类似ComboBox可编辑的下拉框功能
- javascript模拟实现类似c#下的hashtable的简单功能