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 8932851
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T09:24:08+00:00 2026-06-15T09:24:08+00:00

Suppose I have a string that contains a text file, carriage returns and tabs

  • 0

Suppose I have a string that contains a text file, carriage returns and tabs and all. How do I find the index of the first blank line (to include lines-containing-only-whitespace) in that string?

What I’ve tried:

In this case, I have a working function that leverages a bunch of ugly code to find the index of the blank line. There must be a more elegant/readable way to do it than this.

To be clear, the below function returns the section from a string from a supplied ‘title’ to the index of the first blank line after the title. Supplied in full, since most of it is consumed by the search for that index, and to avoid any ‘Why in the WORLD do you need the index of a blank line’ questions. Also to counteract the XY Problem, if it’s happening here.

The (apparently working, haven’t tested all edge cases) code:

// Get subsection indicated by supplied title from supplied section
private static string GetSubSectionText(string section, string subSectionTitle)
    {
        int indexSubSectionBgn = section.IndexOf(subSectionTitle);
        if (indexSubSectionBgn == -1)
            return String.Empty;

        int indexSubSectionEnd = section.Length;

        // Find first blank line after found sub-section
        bool blankLineFound = false;
        int lineStartIndex = 0;
        int lineEndIndex = 0;
        do
        {
            string temp;
            lineEndIndex = section.IndexOf(Environment.NewLine, lineStartIndex);

            if (lineEndIndex == -1)
                temp = section.Substring(lineStartIndex);
            else
                temp = section.Substring(lineStartIndex, (lineEndIndex - lineStartIndex));

            temp = temp.Trim();
            if (temp.Length == 0)
            {
                if (lineEndIndex == -1)
                    indexSubSectionEnd = section.Length;
                else
                    indexSubSectionEnd = lineEndIndex;

                blankLineFound = true;
            }
            else
            {
                lineStartIndex = lineEndIndex + 1;
            }
        } while (!blankLineFound && (lineEndIndex != -1));

        if (blankLineFound)
            return section.Substring(indexSubSectionBgn, indexSubSectionEnd);
        else
            return null;
}

FOLLOW-UP EDIT:

The result (based heavily on Konstantin’s answer):

// Get subsection indicated by supplied title from supplied section
private static string GetSubSectionText(string section, string subSectionTitle)
{
        string[] lines = section.Split(new string[] { Environment.NewLine }, StringSplitOptions.None);
        int subsectStart = 0;
        int subsectEnd = lines.Length;

        // Find subsection start
        for (int i = 0; i < lines.Length; i++)
        {
            if (lines[i].Trim() == subSectionTitle)
            {
                subsectStart = i;
                break;
            }
        }

        // Find subsection end (ie, first blank line)
        for (int i = subsectStart; i < lines.Length; i++)
        {
            if (lines[i].Trim().Length == 0)
            {
                subsectEnd = i;
                break;
            }
        }

        return string.Join(Environment.NewLine, lines, subsectStart, subsectEnd - subsectStart);

}

The primary differences between the result and Konstantin’s answer are due to framework version (I’m working with .NET 2.0, and it doesn’t support string[].Take), and leveraging Environment.NewLine instead of the hardcoded ‘\n’. Much, much prettier and more readable than the original pass. Thanks all!

  • 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-15T09:24:09+00:00Added an answer on June 15, 2026 at 9:24 am

    Have you tried using String.Split Method :

    string s = "safsadfd\r\ndfgfdg\r\n\r\ndfgfgg";
    string[] lines = s.Split('\n');
    int i;
    for (i = 0; i < lines.Length; i++)
    {
        if (string.IsNullOrWhiteSpace(lines[i]))     
        //if (lines[i].Length == 0)          //or maybe this suits better..
        //if (lines[i].Equals(string.Empty)) //or this
        {
            Console.WriteLine(i);
            break;
        }
    }
    Console.WriteLine(string.Join("\n",lines.Take(i)));
    

    EDIT: responding the OP’s edit.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Suppose I have a string that contains Ü. How would I find all those
I have a text file that contains two lines of numbers, all I want
Suppose I have a string of text, of all characters Latin-based. With punctuation. How
Suppose I have an InputStream that contains text data, and I want to convert
I run across this problem frequently suppose I have a text file that I
Suppose you have a dictionary that contains valid words. Given an input string with
Suppose, I have the following string: string temp = "some string contains text which
I have a string that contains the following text String my_string = hello world.
I have some text in a string, I need to check whether that particular
Suppose I have a String message: you should upload file.zip to http://google.com/extremelylonglink.zip , not

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.