dusmanass dusmanass - 1 year ago 52
C# Question

Finding a line in the file where a specific string begins?

I came up with an issue finding the index of the line where a specific string (sentence) begins. May it seem easy, but when it comes to multi-line sentence, things do not get that easy.

To illustrate this, presume, this a text file:

1. The very first sentence. Second
2. sentence (multi-line).

So, let's say I have second sentence assigned to a string:

string sentence = "Second sentence (multi-line)."

And what I want to do is to find the index of a line where my sentence begins. In this case, output should be equals 1.

My code so far:

static int GetSentenceLocation(string fileName, string sentence)
string[] lines = File.ReadAllLines(@fileName);
string[] words = sentence.TrimStart().Split(' ');
int location = 0;

for (int i = 0; i < lines.Length; i++)
if (Regex.IsMatch(lines[i], words[0]))
location = i;
return ++location;

But it does not work properly since the same word may repeat in other sentences.
Any ideas on how to overcome the issue? :)

Answer Source

This problem may be very complicated, but for your test case, something like this may work. You just have to join all the rows in one string variable. Then look for the index of the sentence and finally, look in what line does that index fall. Something like this:

static int GetSentenceLocation()
    string[] lines = new string[2];
    lines[0] = "The very first sentence. Second";
    lines[1] = "sentence (multi-line).";

    string sentence = "Second sentence (multi-line).";

    string alltext = string.Join(" ", lines);

    int index = alltext.IndexOf(sentence);

    int charCount = 0;
    for (int i = 0; i < lines.Length; i++)
        charCount += lines[i].Length;
        if (charCount > index)
            return i + 1;
    return -1;

Of course this code can be optimized, but i hope you get the idea.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download