Getting a line of text after a specific line in C #

I am reading the contents of a file that looks something like this.

[1]
Message=my string here
EffDate=1/1/1995
DeactDate= 
Modified= 
Note=

[2]
Message=my other string here
EffDate=1/1/1995
DeactDate= 
Modified= 
Note=

I want to get the line after Message=, but only until the end of the line. I don't need dates, notes, or anything.

I was hoping to put both messages in an array of strings or something like that. Is there any way to do this?

Any help is appreciated.

+4
source share
7 answers

This is the working code you need:

var d = File.ReadAllLines(filePath);
            var t = d.Where(g => g.Contains("Message"));
            string[] splited;
            foreach (var item in t)
            {
                splited = item.Split(new string[] { "Message=" }, StringSplitOptions.None);
                Console.WriteLine(splited[1]);
            }

Let me know if you have any confusion.

+2
source

You can use followin approach.

  • Read your file in the best way using ReadAllLines method, see link

  • After reading this, you will get a collection of strings, one item for each row.

  • ,
  • , String.Split "=" 1 .

, .

+1

-

string[] getMessages(string file) {
    List<string> messages = new List<string>();

    string[] lines = file.ReadAllLines();       /* read the lines */
    foreach(string line in lines) {
        string[] parts = line.Split('=');
        string contentPart = String.Join("=", parts.Skip(1).ToArray();
        if(parts[0]=="Message")
            messages.Add(contentPart);
    }

    return messages.ToArray();
}

, . , .

, Split : , Message, StartsWith Contains.

+1

, , Message, = .

string strFilePath = @"D:\Maraj\Work\Ajax.txt";
FileStream objFS = new FileStream(strFilePath, FileMode.Open);
List<string> list = new List<string>();
using (StreamReader Sr = new StreamReader(objFS))
  {
    while (Sr.ReadLine() != null)
    {
    string line = Sr.ReadLine();
    if(line.Contains("Message=")
      {
        list.Add(line .Split('=')[1]);
      }
    }
     objFS.Close();
  }
+1
  • .
  • , Message = .
  • If yes, get the message using the Substring () method

        var messages = File.ReadAllLines("M:\\m.txt");
        List<string> result = new List<string>();
        foreach (var msg in messages)
        {
            var key = "Message=";
            if (msg.StartsWith(key))
                result.Add(msg.Substring(msg.IndexOf(key) + key.Length));
        }
    
0
source

You just need a simple loop for:

string[] messages = File.ReadAllLines("data.txt"); //read all lines

for (int i = 1; i < messages.Length; i = i + 7) //just get the line containing "Message"
{
    string message = messages[i].Split('=')[1];
    //now you have the message, save it where ever you want.
}
0
source

That should be enough:

string[] messages = File.ReadLines("fileName.txt")
                        .Where(line => line.StartsWith("Message="))
                        .Select(line => line.Split(new char[] { '=' }, 2)[1])
                        .ToArray();
0
source

All Articles