[英]string.Format with string.Join
I've tried making a string like this:我试过制作这样的字符串:
[1][2][3][4][5][6][7][8][9][10]
With this code:使用此代码:
string nums = "[" + string.Join("][", Enumerable.Range(1, 10)) + "]";
That, however, doesn't really look very good, so I was wondering if I could combine string.Format with string.Join , sort of like this:但是,这看起来并不是很好,所以我想知道是否可以将string.Format与string.Join结合起来,有点像这样:
string num = string.Join("[{0}]", Enumerable.Range(1, 10));
So that it wraps something around each item.这样它就可以在每个项目周围包裹一些东西。 However, that ends up like this:
然而,结果是这样的:
1[{0}]2[{0}]3[{0}]4[{0}]5[{0}]6[{0}]7[{0}]8[{0}]9[{0}]10
Is there a better/easier way to do this?有没有更好/更简单的方法来做到这一点?
Among all the solutions, I must say I prefer this在所有解决方案中,我必须说我更喜欢这个
string s = string.Concat(Enumerable.Range(1, 4).Select(i => string.Format("SomeTitle: >>> {0} <<<\n", i)));
Over this超过这个
string s2 = "SomeTitle: >>>" + string.Join("<<<\nSomeTitle: >>>", Enumerable.Range(1, 4)) + "<<<\n";
Because all the formatting is done in one string, not in multiple.因为所有格式都是在一个字符串中完成的,而不是多个。
string.Concat(Enumerable.Range(1,10).Select(i => string.Format("[{0}]", i)))
I wanted something like this, but with a possibility to enter a format string and a separator.我想要这样的东西,但可以输入格式字符串和分隔符。 So this is what I came up with:
所以这就是我想出的:
public static string JoinFormat<T>(this IEnumerable<T> list, string separator,
string formatString)
{
formatString = string.IsNullOrWhiteSpace(formatString) ? "{0}": formatString;
return string.Join(separator,
list.Select(item => string.Format(formatString, item)));
}
Now you could make a list like现在你可以列一个清单
[1], [2], [3], [4], [5], [6], [7], [8], [9], [10]
[1], [2], [3], [4], [5], [6], [7], [8], [9], [10]
by entering numbers.JoinFormat(", ", "[{0}]")
.通过输入
numbers.JoinFormat(", ", "[{0}]")
。
Whereas a Concat
solution with "[{0}],")
would have a trailing comma.而带有
"[{0}],")
的Concat
解决方案将有一个尾随逗号。
An empty or null
separator produces your list. null
分隔符或null
分隔符会生成您的列表。
You are probably looking for a LINQ solution such as您可能正在寻找 LINQ 解决方案,例如
string nums = String.Concat(Enumerable.Range(1, 10)
.Select(i => string.Format("[{0}]", i)))
I'd just concatenate each item, and use String.Concat
to put them together:我只是连接每个项目,并使用
String.Concat
将它们放在一起:
string num =
String.Concat(
Enumerable.Range(1, 10).Select(n => "[" + n + "]")
);
If you want to get fancy, you can make a cross join between the numbers and a string array.如果你想花哨,你可以在数字和字符串数组之间进行交叉连接。 :)
:)
string num =
String.Concat(
from n in Enumerable.Range(1, 10)
from s in new string[] { "[", null, "]" }
select s ?? n.ToString()
);
StringBuilder str = new StringBuilder();
for (int i = 1; i <= 10; i++)
str.AppendFormat("[{0}]", i);
Console.WriteLine(str.ToString());
My recommendation is to use StringBuilder to append the same pattern.我的建议是使用 StringBuilder 来附加相同的模式。
string.Join(',', Enumerable.Range(1, 10).Select(i => string.Format("'{0}'", i)))
String Join can be better option than Concat字符串连接可能是比 Concat 更好的选择
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.