Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7862459
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T23:08:07+00:00 2026-06-02T23:08:07+00:00

In a Windows Forms C# app, I have a textbox where users paste log

  • 0

In a Windows Forms C# app, I have a textbox where users paste log data, and it sorts it. I need to check each line individualy so I split the input by the new line, but if there are a lot of lines, greater than 100,000 or so, it throws a OutOfMemoryException.

My code looks like this:

StringSplitOptions splitOptions = new StringSplitOptions();
if(removeEmptyLines_CB.Checked)
    splitOptions = StringSplitOptions.RemoveEmptyEntries;
else
    splitOptions = StringSplitOptions.None;

List<string> outputLines = new List<string>();

foreach(string line in input_TB.Text.Split(new string[] { "\r\n", "\n" }, splitOptions))
{
    if(line.Contains(inputCompare_TB.Text))
        outputLines.Add(line);
}
output_TB.Text = string.Join(Environment.NewLine, outputLines);

The problem comes from when I split the textbox text by line, here input_TB.Text.Split(new string[] { "\r\n", "\n" }

Is there a better way to do this? I’ve thought about taking the first X amount of text, truncating at a new line and repeat until everything has been read, but this seems tedious. Or is there a way to allocate more memory for it?

Thanks,
Garrett

Update

Thanks to Attila, I came up with this and it seems to work. Thanks

StringReader reader = new StringReader(input_TB.Text);
string line;
while((line = reader.ReadLine()) != null)
{
    if(line.Contains(inputCompare_TB.Text))
        outputLines.Add(line);
}
output_TB.Text = string.Join(Environment.NewLine, outputLines);
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-02T23:08:10+00:00Added an answer on June 2, 2026 at 11:08 pm

    The better way to do this would be to extract and process one line at a time, and use a StringBuilder to create the result:

    StringBuilder outputTxt = new StringBuilder();
    string txt = input_TB.Text;
    int txtIndex = 0;
    while (txtIndex < txt.Length) {
      int startLineIndex = txtIndex;
    GetMore:
      while (txtIndex < txt.Length && txt[txtIndex] != '\r'  && txt[txtIndex] != '\n')) {
        txtIndex++;
      }
      if (txtIndex < txt.Length && txt[txtIndex] == '\r' && (txtIndex == txt.Length-1 || txt[txtIndex+1] != '\n') {
        txtIndex++;
        goto GetMore; 
      }
      string line = txt.Substring(startLineIndex, txtIndex-startLineIndex);
      if (line.Contains(inputCompare_TB.Text)) {
        if (outputTxt.Length > 0)
          outputTxt.Append(Environment.NewLine);
        outputTxt.Append(line); 
      }
      txtIndex++;
    } 
    output_TB.Text = outputTxt.ToString(); 
    

    Pre-emptive comment: someone will object to the goto – but it is what’s needed here, the alternatives are much more complex (reg exp for example), or fake the goto with another loop and continue or break

    Using a StringReader to split the lines is a much cleaner solution, but it does not handle both \r\n and \n as a new line:

    StringReader reader = new StringReader(input_TB.Text); 
    StringBuilder outputTxt = new StringBuilder();
    string compareTxt = inputCompare_TB.Text;
    string line; 
    while((line = reader.ReadLine()) != null) { 
      if (line.Contains(compareTxt)) {
        if (outputTxt.Length > 0)
          outputTxt.Append(Environment.NewLine);
        outputTxt.Append(line); 
      }
    } 
    output_TB.Text = outputTxt.ToString(); 
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a windows forms app with a textbox control that I want to
I have a windows forms app where I have split different functionality into several
I have a Windows Forms app that displays a list of objects in a
I have a Windows Forms app, that has a single ElementHost containing a WPF
I have a .NET 2.0 windows forms app, which makes heavy use of the
I have created a windows installer for a windows forms app as an MSI.
I'm moving my old app from windows forms to WPF and have encountered an
I have a .NET application that contains a checkbox (System.Windows.Forms.Checkbox). This component (WindowsForms10.BUTTON.app.0.378734a1) is
I have a windows forms app in C#. Platform is vS 2005. Following is
I have a Windows forms app in .Net 4.0. I work in C#. I

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.