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!
Have you tried using String.Split Method :
EDIT: responding the OP’s edit.