字符串用复数字符串连接



我正在尝试用List连接我的字符串。

例如:

ID Number, List string 1, List string 2...

因为我需要"在字符串之间。所以我使用字符串联接,但它不起作用。下面是我的代码。

我的代码:

String IDnumber = string.Join(", ", owner.ID , customerID.Select(x => x.ID).ToList());

我想知道是否有什么变通办法。

在您的示例中,您似乎正在使用

public static string Join (char separator, params object?[] values)

因此,您本质上是在调用

string.Join(", ", [first part], [second part])

如果你的owner.ID是123,加上你的客户列表,我想你的输出看起来像这个

123, System.Collections.Generic.List`1[System.String]

这是因为您不是将owner.ID和客户列表中的每个元素连接在一起,而是将owner.ID和整个客户列表作为一个对象。

正确的方法是类似

string.Join(", ", customerID.Select(x => x.ID).Prepend(owner.ID))

$"{owner.ID}, {string.Join(", ", customerID.Select(x => x.ID))}"

new StringBuilder().Append(owner.ID).Append(", ").Append(string.Join(", ", customerID.Select(x => x.ID))).ToString()

最新更新