填充多个printf格式占位符相同的值 [英] Populating multiple printf format placeholders with same value

查看:418
本文介绍了填充多个printf格式占位符相同的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用.NET字符串格式化您可以将相同的值转换成格式字符串多次:

Using .NET string formatting you can plug the same value into a format string multiple times:

Console.Write("{0}{0}{0}", 1) //prints "111"

有没有办法用printf风格的格式化做到这一点,提供的价值只有一次?

Is there any way to do this with printf-style formatting, supplying the value only once?

推荐答案

没有。的值取在顺序,从堆栈中,当传递给函数。如果你想相同的值如果字符串的不同部分的多次出现,你必须提供它们才能,多次。

No. The values are taken in order, from the stack, when passed to the function. If you want multiple appearances of the same value if different parts of the string, you have to supply them in order, multiple times.

想想这样说:你有一个字符串,用彩笔和东西插入替换这些标记列表。由于这份名单是在出场顺序,如果你想更换两个标志具有相同的值,该值已经显示了两次。

Think of it this way: you have a string, with markers, and a list of things to insert replacing those markers. As the list has to be in order of appearance, if you want to replace two markers with the same value, the value has to show up twice.

现在,请记住,重复的参数并不一定意味着重复的实际数据。

Now, keep in mind that duplicating the parameter doesn't necessarily mean duplicating the actual data.

这篇关于填充多个printf格式占位符相同的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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