拆分512个char块中的字符串

时间:2009-10-27 16:33:24

标签: c# string split

也许是一个基本的问题,但是让我们说我有一个长度为2000个字符的字符串,我需要将此字符串拆分为每个最多512个字符的块。

有没有一个很好的方法,比如循环左右这样做?

8 个答案:

答案 0 :(得分:20)

这样的事情:

private IList<string> SplitIntoChunks(string text, int chunkSize)
{
    List<string> chunks = new List<string>();
    int offset = 0;
    while (offset < text.Length)
    {
        int size = Math.Min(chunkSize, text.Length - offset);
        chunks.Add(text.Substring(offset, size));
        offset += size;
    }
    return chunks;
}

或者只是迭代:

private IEnumerable<string> SplitIntoChunks(string text, int chunkSize)
{
    int offset = 0;
    while (offset < text.Length)
    {
        int size = Math.Min(chunkSize, text.Length - offset);
        yield return text.Substring(offset, size);
        offset += size;
    }
}

请注意,这会拆分成UTF-16代码单元的块,这与分割成Unicode代码点的块不完全相同,而这些代码点又可能与分割成字形块不同。

答案 1 :(得分:3)

使用Jon的实现和 yield 关键字。

IEnumerable<string> Chunks(string text, int chunkSize)
{
    for (int offset = 0; offset < text.Length; offset += chunkSize)
    {
        int size = Math.Min(chunkSize, text.Length - offset);
        yield return text.Substring(offset, size);
    }
}

答案 2 :(得分:3)

虽然这个问题同时有一个公认的答案,但这里有一个简短的版本,有正则表达式的帮助。纯粹主义者可能不喜欢它(可以理解),但是当你需要快速解决方案并且你对正则表达式很方便时,就可以了。令人惊讶的是,表现相当不错:

string [] split = Regex.Split(yourString, @"(?<=\G.{512})");

它做什么?负面向后看并记住\G的最后一个位置。它也将捕获最后一位,即使它不能被512分割。

答案 3 :(得分:1)

static IEnumerable<string> Split(string str, int chunkSize)    
{   
    int len = str.Length;
    return Enumerable.Range(0, len / chunkSize).Select(i => str.Substring(i * chunkSize, chunkSize));    
}

来源:Splitting a string into chunks of a certain size

答案 4 :(得分:1)

基于string类型实现IEnumerable<char>的事实,我敢于提供更多LINQified版本的Jon解决方案:

private IList<string> SplitIntoChunks(string text, int chunkSize)
{
    var chunks = new List<string>();
    int offset = 0;
    while(offset < text.Length) {
        chunks.Add(new string(text.Skip(offset).Take(chunkSize).ToArray()));
        offset += chunkSize;
    }
    return chunks;
}

答案 5 :(得分:1)

大多数答案可能都有相同的缺陷。 如果文本为空,则不会产生任何结果。 我们(我)期望至少回到那个空字符串(与不在字符串中的字符串上的拆分行为相同,这将返回一个项目:给定字符串)

所以我们应该至少循环一次(基于Jon的代码):

IEnumerable<string> SplitIntoChunks (string text, int chunkSize)
{
    int offset = 0;
    do
    {
        int size = Math.Min (chunkSize, text.Length - offset);
        yield return text.Substring (offset, size);
        offset += size;
    } while (offset < text.Length);
}

或使用for(已编辑:经过多次使用此操作后,我找到了更好的方法来处理 chunkSize大于文本的情况):

IEnumerable<string> SplitIntoChunks (string text, int chunkSize)
{
    if (text.Length <= chunkSize)
        yield return text;
    else
    {
        var chunkCount = text.Length / chunkSize;
        var remainingSize = text.Length % chunkSize;

        for (var offset = 0; offset < chunkCount; ++offset)
            yield return text.Substring (offset * chunkSize, chunkSize);

        // yield remaining text if any
        if (remainingSize != 0)
            yield return text.Substring (chunkCount * chunkSize, remainingSize);
    }
}

也可以与do / while循环一起使用;)

答案 6 :(得分:0)

通用扩展方法:

>  <entry>
    <record>177</record>
    <time>2017/09/08 08:42:14.686</time>
    <type>Error</type>
    <source>Editor or Editor Extension</source>
    <description>System.OutOfMemoryException: Exception of type &apos;System.OutOfMemoryException&apos; was thrown.&#x000D;&#x000A;   at System.String.CtorCharArray(Char[] value)&#x000D;&#x000A;   at Microsoft.VisualStudio.Text.Implementation.BinaryStringRebuilder.GetText(Span span)&#x000D;&#x000A;   at Microsoft.VisualStudio.Text.Implementation.TextSnapshot.GetText(Span span)&#x000D;&#x000A;   at Microsoft.VisualStudio.Text.SnapshotSpan.GetText()&#x000D;&#x000A;   at Microsoft.VisualStudio.Editor.Implementation.TextDocData.GetLineText(Int32 iStartLine, Int32 iStartIndex, Int32 iEndLine, Int32 iEndIndex, String&amp; pbstrBuf)&#x000D;&#x000A;   at Microsoft.VisualStudio.Package.Source.GetText()&#x000D;&#x000A;   at Microsoft.VisualStudio.Package.Source.BeginParse(Int32 line, Int32 idx, TokenInfo info, ParseReason reason, IVsTextView view, ParseResultHandler callback)&#x000D;&#x000A;   at Microsoft.VisualStudio.Package.ViewFilter.GetDataTipText(TextSpan[] aspan, String&amp; textValue)&#x000D;&#x000A;   at Microsoft.VisualStudio.Editor.Implementation.ShimQuickInfoSource.TryGetQuickInfoFromFilter(IQuickInfoSession session, TextSpan[] dataBufferTextSpan, String&amp; tipText)&#x000D;&#x000A;   at Microsoft.VisualStudio.Editor.Implementation.ShimQuickInfoSource.AugmentQuickInfoSession(IQuickInfoSession session, IList`1 qiContent, ITrackingSpan&amp; applicableToSpan)&#x000D;&#x000A;   at Microsoft.VisualStudio.Language.Intellisense.Implementation.QuickInfoSession.Recalculate()&#x000D;&#x000A;   at Microsoft.VisualStudio.Language.Intellisense.Implementation.QuickInfoSession.Start()&#x000D;&#x000A;   at Microsoft.VisualStudio.Language.Intellisense.Implementation.DefaultQuickInfoController.OnTextView_MouseHover(Object sender, MouseHoverEventArgs e)&#x000D;&#x000A;   at Microsoft.VisualStudio.Text.Editor.Implementation.WpfTextView.RaiseHoverEvents()</description>
  </entry>

答案 7 :(得分:-1)

喜欢什么?

Calculate eachLength = StringLength / WantedCharLength
Then for (int i = 0; i < StringLength; i += eachLength)
SubString (i, eachLength);