Populating multiple printf format placeholders with same value

淺唱寂寞╮ 提交于 2020-01-14 18:48:11

问题


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

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

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


回答1:


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.



来源:https://stackoverflow.com/questions/4055803/populating-multiple-printf-format-placeholders-with-same-value

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!