Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/string/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 在字符串列表c中每行的字符之间添加换行符#_C#_String_List - Fatal编程技术网

C# 在字符串列表c中每行的字符之间添加换行符#

C# 在字符串列表c中每行的字符之间添加换行符#,c#,string,list,C#,String,List,我有一个包含任意行数的字符串列表。每行12个字符长,我想将其内容打印到文本文件中。这是相当容易做到的 System.IO.File.WriteAllLines(@".\strings.txt", myList); 现在,我想在每6个字符后插入一个换行符,实际上是列表计数的两倍 例如 使用集合,您可以做出一些很好的假设并打印子字符串。考虑下面的例子: using System; using System.Collections.Generic; using System.Linq; using

我有一个包含任意行数的字符串列表。每行12个字符长,我想将其内容打印到文本文件中。这是相当容易做到的

System.IO.File.WriteAllLines(@".\strings.txt", myList);
现在,我想在每6个字符后插入一个
换行符
,实际上是列表计数的两倍

例如


使用集合,您可以做出一些很好的假设并打印子字符串。考虑下面的例子:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace StringSplit
{
    class Program
    {
        static void Main(string[] args)
        {
            string input = @"123456789ABC
123456789ABC";

            string[] lines = input.Split(new char[]{'\r', '\n'}, StringSplitOptions.RemoveEmptyEntries);
            foreach (var l in lines)
            {
                System.Diagnostics.Debug.WriteLine(l.Substring(0, 6));
                System.Diagnostics.Debug.WriteLine(l.Substring(6, 6));
            }
        }
    }
}
输出:

123456
789ABC
123456
789ABC
或者,如果您知道每行都有12个字符:

System.IO.File.WriteAllLines(@".\strings.txt", myList.Select(x => x.Insert(6, Environment.NewLine)));

你可能有兴趣取悦下一个投票人,为下一个投票添加一个理由,我会努力改进我的问题。谢谢你。
x=>…
是如何工作的?不管怎样,它解决了我的问题。另外,我怎样才能将它存储在一个新的列表中,而不是将其写入文件?非常感谢,先生!编辑:算出,追加
ToList()
System.IO.File.WriteAllLines(@".\strings.txt", myList.Select(x => x.Length > 6 ? x.Insert(6, Environment.NewLine) : x));
System.IO.File.WriteAllLines(@".\strings.txt", myList.Select(x => x.Insert(6, Environment.NewLine)));