1

I have a string which contains so many lines in it.Now as per my requirement i have to search a substring(text) into this string and find out the line number in which this substring(text) exists in the string.

Once i will get the line number i have to read that line and get to know Which contents in it is Character and what is integer or digits.

This is the code that i am using to read a specific line ..

private static string ReadLine(string text, int lineNumber)
{
    var reader = new StringReader(text);

    string line;
    int currentLineNumber = 0;

    do
    {
        currentLineNumber += 1;
        line = reader.ReadLine();
    }
    while (line != null && currentLineNumber < lineNumber);

    return (currentLineNumber == lineNumber) ? line : string.Empty;
}

But how to search the line number which contain specific text(substring)?

Wai Ha Lee
  • 8,598
  • 83
  • 57
  • 92
user3924730
  • 163
  • 1
  • 3
  • 14
  • This may help: http://stackoverflow.com/questions/15786612/read-text-file-at-specific-line – sr28 Aug 11 '14 at 09:11
  • @sr28 I wont have to read specific line of textfile.That is being done by posted code correctly instead i have to get the line number which contains specific text – user3924730 Aug 11 '14 at 09:14
  • It would look very similar to your posted code... Just a little different conditions. – H H Aug 11 '14 at 09:30

2 Answers2

4

OK i will simplify.How to get line number of a specific text present in a string in c#

Then you could use this method:

public static int GetLineNumber(string text, string lineToFind, StringComparison comparison = StringComparison.CurrentCulture)
{
    int lineNum = 0;
    using (StringReader reader = new StringReader(text))
    {
        string line;
        while ((line = reader.ReadLine()) != null)
        {
            lineNum++;
            if(line.Equals(lineToFind, comparison))
                return lineNum;
        }
    }
    return -1;
}
Tim Schmelter
  • 450,073
  • 74
  • 686
  • 939
  • OK i will simplify.How to get line number of a specific text present in a string in c# – user3924730 Aug 11 '14 at 09:18
  • What argument i need to pass for `StringComparison comparison = StringComparison.CurrentCulture` – user3924730 Aug 11 '14 at 09:29
  • @user3924730: either none (it's optional) or one of [these](http://msdn.microsoft.com/en-us/library/system.stringcomparison(v=vs.110).aspx). If you for example want to compare case-insensitive(so `"Sample Line"` == `"sample line"`) you could use `StringComparison.CurrentCultureIgnoreCase`. Default is a case sensitive comparison. – Tim Schmelter Aug 11 '14 at 09:31
  • `if(line.Equals(lineToFind, comparison));` i am getting an error of possible mistaken empty statement and now i am not getting values also.Why?.The function is not working at all! – user3924730 Aug 11 '14 at 09:49
  • @user3924730: there was a semicolon behind the `if`, i've corrected it. – Tim Schmelter Aug 11 '14 at 10:02
  • I changed line.Equals to line.StartsWith to find the first line that starts with a particular substring. I also used File.ReadAllText so I can use this function on a file. – humbads Jan 26 '20 at 19:23
0

I know this is already solved and old but I want to share an alternative to the solved answer because I couldn't get it to work for simple things. The code just returns the line number where it founds a part of the string given, to have the exact one just replace "Contains" with "Equals".

public int GetLineNumber(string lineToFind) {        
    int lineNum = 0;
    string line;
    System.IO.StreamReader file = new System.IO.StreamReader("c:\\test.txt");
    while ((line = file.ReadLine()) != null) {
        lineNum++;
        if (line.Contains(lineToFind)) {
            return lineNum;
        }
    }
    file.Close();
    return -1;
}
John M.
  • 76
  • 1
  • 4
  • 11