简体   繁体   English

如何使用string.Join()联接多个子字符串?

[英]How can I use string.Join() to join multiple substrings?

I'm looping through some records, and flagging errors if there are difference using .Except Everything works great, except I want to make a more user friendly error message. 我正在遍历一些记录,并使用.Except标记(如果有区别的话),它们会标记错误。除一切都很好外,我想做出一个更加用户友好的错误消息。

For failedTests , I only want to add the first characters of each line in errorRecords (There may be several lines.) If I do failedTests = string.Join("\\n", errorRecords).Substring(0,10); 对于failedTests ,我只想在errorRecords添加每行的首字符(可能有几行。)如果我这样做了, failedTests = string.Join("\\n", errorRecords).Substring(0,10); I only get the substring for the first row. 我只得到第一行的子字符串。 I need the substring for each row. 我需要每一行的子字符串。

Is there a quick and dirty way to do this? 有没有一种快速而肮脏的方法来做到这一点?

  var errorRecords = sourceList.Except(destList);
  failedTests = string.Join("\n", errorRecords);

我建议使用简单的LINQ语句创建每行的修改版本,然后将其传递给Join。

failedTests = string.Join("\n", errorRecords.Select(r => r.Substring(1, 10));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM