您的位置:首页 > 编程语言 > C语言/C++

在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'

函数代码:

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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: