带有“bla {0} bla”语法的string.Format()非常棒。 但有时我不想枚举占位符。 相反,我只是想在变量中依次映射变量。 有没有可以做到这一点的图书馆?
例如,而不是
string.Format("string1={0}, string2={1}", v1, v2)就像是
string.Format("string1={*}, string2={*}", v1, v2)string.Format() with it's "bla {0} bla" syntax is great. But sometimes I don't want to enumerate the placeholders. Instead I just want to map the variables sequentially in the placeholders. Is there a library that can do that?
For instance, instead of
string.Format("string1={0}, string2={1}", v1, v2)something like
string.Format("string1={*}, string2={*}", v1, v2)最满意答案
假设你使用的是.NET 3.5或更高版本,你可以通过编写自己的字符串扩展和params关键字来完成。
编辑:无聊,代码是草率和容易出错的,但把这个类放在你的项目中,并在必要时使用它的命名空间:
public static class StringExtensions { public static string FormatEx(this string s, params string[] parameters) { Regex r = new Regex(Regex.Escape("{*}")); for (int i = 0; i < parameters.Length; i++) { s = r.Replace(s, parameters[i], 1); } return s; } }用法:
Console.WriteLine("great new {*} function {*}".FormatEx("one", "two"));You could accomplish this yourself by writing your own string extension coupled with the params keyword, assuming you're using .NET 3.5 or higher.
Edit: Got bored, the code is sloppy and error prone, but put this class in your project and using its namespace if necessary:
public static class StringExtensions { public static string FormatEx(this string s, params string[] parameters) { Regex r = new Regex(Regex.Escape("{*}")); for (int i = 0; i < parameters.Length; i++) { s = r.Replace(s, parameters[i], 1); } return s; } }Usage:
Console.WriteLine("great new {*} function {*}".FormatEx("one", "two"));在.net中替代string.Format(“... {0} ... {1} ...”,v1,v2)(alternatives to string.Format(“… {0}…{1}…”,v1,v2) in .net?)带有“bla {0} bla”语法的string.Format()非常棒。 但有时我不想枚举占位符。 相反,我只是想在变量中依次映射变量。 有没有可以做到这一点的图书馆?
例如,而不是
string.Format("string1={0}, string2={1}", v1, v2)就像是
string.Format("string1={*}, string2={*}", v1, v2)string.Format() with it's "bla {0} bla" syntax is great. But sometimes I don't want to enumerate the placeholders. Instead I just want to map the variables sequentially in the placeholders. Is there a library that can do that?
For instance, instead of
string.Format("string1={0}, string2={1}", v1, v2)something like
string.Format("string1={*}, string2={*}", v1, v2)最满意答案
假设你使用的是.NET 3.5或更高版本,你可以通过编写自己的字符串扩展和params关键字来完成。
编辑:无聊,代码是草率和容易出错的,但把这个类放在你的项目中,并在必要时使用它的命名空间:
public static class StringExtensions { public static string FormatEx(this string s, params string[] parameters) { Regex r = new Regex(Regex.Escape("{*}")); for (int i = 0; i < parameters.Length; i++) { s = r.Replace(s, parameters[i], 1); } return s; } }用法:
Console.WriteLine("great new {*} function {*}".FormatEx("one", "two"));You could accomplish this yourself by writing your own string extension coupled with the params keyword, assuming you're using .NET 3.5 or higher.
Edit: Got bored, the code is sloppy and error prone, but put this class in your project and using its namespace if necessary:
public static class StringExtensions { public static string FormatEx(this string s, params string[] parameters) { Regex r = new Regex(Regex.Escape("{*}")); for (int i = 0; i < parameters.Length; i++) { s = r.Replace(s, parameters[i], 1); } return s; } }Usage:
Console.WriteLine("great new {*} function {*}".FormatEx("one", "two"));
发布评论