拆分一个巨大的文字成小块的最快方法 [英] Fastest way to split a huge text into smaller chunks

查看:186
本文介绍了拆分一个巨大的文字成小块的最快方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我用下面的代码来分割字符串,但它需要大量的时间。

I have used the below code to split the string, but it takes a lot of time.

using (StreamReader srSegmentData = new StreamReader(fileNamePath))
{
    string strSegmentData = "";
    string line = srSegmentData.ReadToEnd();
    int startPos = 0;

    ArrayList alSegments = new ArrayList();
    while (startPos < line.Length && (line.Length - startPos) >= segmentSize)
    {
        strSegmentData = strSegmentData + line.Substring(startPos, segmentSize) + Environment.NewLine;
        alSegments.Add(line.Substring(startPos, segmentSize) + Environment.NewLine);
        startPos = startPos + segmentSize;
    }
}

请给我建议用另一种方式的字符串分割成固定大小的小块

Please suggest me an alternative way to split the string into smaller chunks of fixed size

推荐答案

首先你应该确定你的意思的什么块大小的。如果你的意思的具有固定数量的代码单元块 的那么你实际的算法可能是缓慢的,但它的工作原理。如果这不是你想要什么,你实际上意味着的具有固定数量的字符块 的那么它是坏了。我讨论这个代码审查后一个类似的问题:拆分字符串转换成相同长度的大块那么我会重复这里只相关部分。

First of all you should define what you mean with chunk size. If you mean chunks with a fixed number of code units then your actual algorithm may be slow but it works. If it's not what you intend and you actually mean chunks with a fixed number of characters then it's broken. I discussed a similar issue in this Code Review post: Split a string into chunks of the same length then I will repeat here only relevant parts.


  • 您是超过分区字符,但字符串是UTF-16编码的,那么你可能会产生的的中,至少有3例字符串:

  • You're partitioning over Char but String is UTF-16 encoded then you may produce broken strings in, at least, three cases:

这篇关于拆分一个巨大的文字成小块的最快方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆