简体   繁体   中英

C# Split a string in four equal substrings but only at newline

I received an out of memory exception with the following code which splits a large string (the str variable) into lines and enqueues them:

 foreach (
            var value in
                str.Split(
                    new string[] { System.Environment.NewLine.ToString(CultureInfo.InvariantCulture) },
                       StringSplitOptions.None))
                {
                    lines.Enqueue(value);
                }

My solution (which I'm not sure is a good one) is to first split the large string (the str variable) into 4 chunks and then split each chunk separately and enqueue the resulting lines in the lines queue. The problem I'm having is figuring out how to split on a newline so my eventual lines queue contains only complete lines.

I wrote the following code to split the large string (the str variable) into 4 substrings but how can I change it to split on a newline only?

      int chunkNum = 4;
      int chunkLength = str.Length/chunkNum;
      int stringLength = str.Length;
      var j = 0;
          for (var i = 0; i < stringLength; i += chunkLength)
           {
              if (j == (chunkNum - 1))
              {
                  chunkLength = stringLength - i;
              }

              chunkQueue.Enqueue(str.Substring(i, chunkLength + i));
              j++;
            }

You could use StreamReader which provides ReadLine method.

string text = "Split\nby\nnewline", line = "";
using( StreamReader sr = new StreamReader( new MemoryStream( Encoding.UTF8.GetBytes( text ) ) ) ) {
    while( ( line = sr.ReadLine() ) != null )
        Console.WriteLine( line );
}

Does that suite your needs? Just enqueue the string instead of printing it.

If the input string is that huge, you probably got it from a Stream (file?) anyway. Maybe you could just read that stream character by character, append to a Stringbuilder, and Enqueue when the character is a newline?

Ah, just missed pikausp's answer, which makes a lot more sense...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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