find a string in text file and create bytes from The rest of the file c#

96 views Asked by At

i have a text file contain many line like that:

text1
text2
text3
text4
text5
text6

i want to find "text3" and create byte array from The rest of the file

1

There are 1 answers

4
Megha On

Check this code. Here the list Valinbytes is the list that will contain the array of bytes for all values after text3. You can easily convert this to array using the ToArray() method on the list.Feel free to let me know if you need something more from it.

    static void Main(string[] args)
   {
        string[] text =  System.IO.File.ReadAllText(@"D:\check.txt").Split(new
         string[] { System.Environment.NewLine },                                                                                       System.StringSplitOptions.RemoveEmptyEntries);
         List<byte[]> Valinbytes = new List<byte[]>();
         int Valtosplit = 0;
         for (int i = 0; i < text.Length;i++)
          {
             if (text[i].ToLower().Equals("text3"))
            {
                Valtosplit = i;
                break;
            }
        }
        for (int j = Valtosplit; j < text.Length; j++)
        {
            byte[] byteval = Encoding.UTF8.GetBytes(text[j]);
            Valinbytes.Add(byteval);
        }
    }