格式化字符串写入变量中

motefancysilver 2012-09-19 05:24:16
在PHP 中 的vsprintf(format,argarray)函数 。数组的元素会被插入主字符串的百分比 (%) 符号处。该函数是逐步执行的。在第一个 % 符号中,插入 arg1,在第二个 % 符号处,插入 arg2,依此类推。

在c# 中有没有 这样的函数????

...全文
76 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
Kilin_Zhang 2012-09-20
  • 打赏
  • 举报
回复
楼主说的是占位符
C#中有 string.Format可以达到效果,这个方法有好多个版本的重载,下面只是其中一个,仅供参考。

//
// Summary:
// Replaces the format item in a specified string with the string representation
// of a corresponding object in a specified array.
//
// Parameters:
// format:
// A composite format string.
//
// args:
// An object array that contains zero or more objects to format.
//
// Returns:
// A copy of format in which the format items have been replaced by the string
// representation of the corresponding objects in args.
//
// Exceptions:
// System.ArgumentNullException:
// format or args is null.
//
// System.FormatException:
// format is invalid.-or- The index of a format item is less than zero, or greater
// than or equal to the length of the args array.
public static string Format(string format, params object[] args);
q107770540 2012-09-19
  • 打赏
  • 举报
回复
如果只是为数组内每个元素添加一个 %
可以这样

argarray=argarray.Select(a=>String.Formate("{0}%",a)).ToArray();
q107770540 2012-09-19
  • 打赏
  • 举报
回复
说了这么多,不如直接举个例子
说明一下格式化前的字符串和格式化后的字符串是什么样子

110,825

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • AIGC Browser
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧