如何在X次之后重复使用分割方法?

huangapple go评论65阅读模式
英文:

How To Repeat Split Method After X Times?

问题

我有一个.txt文件,我想使用split方法进行拆分。我的当前代码是:

string[] alltext = File.ReadAllText(fullPath).Split(new[] { ',' }, 3);

现在我遇到的问题是,我希望它以一种方式循环遍历整个文本,始终将文本拆分成三个属于同一组的部分。如果我有一个文本如下:

testing, testing,  
buenooo diasssss

testing, testing,  
buenooo diasssss

testing, testing,  
buenooo diasssss

(这里的格式很难显示,但我想表明它们位于不同的行,因此按行读取很可能不可行)
我希望“testing”,“testing”,“buenooo diasssss”在我的控制台上显示,尽管它们位于不同的行。

如果我使用行来处理,我只需循环遍历每一行,但在这种情况下这种方法不起作用。

英文:

I have a .txt file which I would like to split using the split method. My current code is:

string[] alltext = File.ReadAllText(fullPath).Split(new[] { ',' }, 3);

The problem I now have is that I want it to loop through the whole in a way that it always splits the text into three pieces that belong together. If I have a text with:

testing, testing,  
buenooo diasssss

testing, testing,  
buenooo diasssss

testing, testing,  
buenooo diasssss

(the format here is hard to display, but want to show that they are on different lines, so reading line by line will most likely not be possible)
I want "testing", "testing", "buenooo diasssss" to be dispalyed on my console althought they are on different lines.

If I would do it with lines I would simply loop through each line, but this does not work in this case.

答案1

得分: 0

你可以首先从文本中移除"\r\n"(换行符),然后分割并选择前三项。

var alltext = File.ReadAllText(fullPath).Replace(""\r\n","").Split(',').ToList().Take(3);
foreach(var item in alltext)
     Console.WriteLine(item);

编辑

如果你想在控制台中显示所有三个项目在同一行:

int lineNumber = 0;
var alltext = File.ReadAllText(fullPath).Split(new string[] { ""\r\n"", "," }, StringSplitOptions.None).ToList();
alltext.RemoveAll(item => item == "");
while (lineNumber * 3 < alltext.Count)
{
    var tempList = alltext.Skip(lineNumber * 3).Take(3).ToList(); ;
    lineNumber++;
    Console.WriteLine("line {0} => {1}, {2}, {3}",lineNumber, tempList[0], tempList[1], tempList[2]);
}

结果:

如何在X次之后重复使用分割方法?

英文:

You can first remove &quot;\r\n&quot;(new line) from the text, then split and select the first three items.

var alltext = File.ReadAllText(fullPath).Replace(&quot;\r\n&quot;,&quot;&quot;).Split(&#39;,&#39;).ToList().Take(3);
foreach(var item in alltext)
     Console.WriteLine(item);

Edit

If you want all three items to be displayed in one line in the console:

        int lineNumber = 0;
        var alltext = File.ReadAllText(fullPath).Split(new string[] { &quot;\r\n&quot;, &quot;,&quot; }, StringSplitOptions.None).ToList();
        alltext.RemoveAll(item =&gt; item == &quot;&quot;);
        while (lineNumber * 3 &lt; alltext.Count)
        {
            var tempList = alltext.Skip(lineNumber * 3).Take(3).ToList(); ;
            lineNumber++;
            Console.WriteLine(&quot;line {0} =&gt; {1}, {2}, {3}&quot;,lineNumber, tempList[0], tempList[1], tempList[2]);
        }

result:

如何在X次之后重复使用分割方法?

答案2

得分: 0

尝试这个:

var data =
    File.ReadLines(fullpath)
        .Select((x, n) => (line: x, group: n / 3))
        .GroupBy(x => x.group, x => x.line)
        .Select(x =>
            string
                .Concat(x)
                .Split(',', StringSplitOptions.RemoveEmptyEntries)
                .Select(x => x.Trim()));

这将给我:

如何在X次之后重复使用分割方法?

英文:

Try this:

var data =
	File.ReadLines(fullpath)
		.Select((x, n) =&gt; (line: x, group: n / 3))
		.GroupBy(x =&gt; x.group, x =&gt; x.line)
		.Select(x =&gt;
			String
				.Concat(x)
				.Split(&#39;,&#39;, StringSplitOptions.RemoveEmptyEntries)
				.Select(x =&gt; x.Trim()));

That gives me:

如何在X次之后重复使用分割方法?

huangapple
  • 本文由 发表于 2023年1月9日 19:36:18
  • 转载请务必保留本文链接:https://go.coder-hub.com/75056712.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定