-2

I need to delete first row from string[] lines. I load file text into str variable. In array string I split text from str where is one line of text new array item. But first line is title who I dont need in further code and I wanna delete or skip that line when I load file. Here is my code:

string url = @"E:\Sims.log";
    Stream stream = File.Open(url, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);

                using (StreamReader sr = new StreamReader(stream))
                {
                    string str = sr.ReadToEnd();
                    string[] lines = Regex.Split(str, "\r\n");
                   //I need to delete first row from lines
                }
ReadyToWork
  • 81
  • 1
  • 10
  • 1
    I can't think of a reason you would need to delete the line instead of just skipping over it by starting with index 1... – entropic Aug 15 '14 at 14:06
  • There's a function ready for you made in this post: [sub-array][1] [1]: http://stackoverflow.com/questions/943635/c-sharp-arrays-getting-a-sub-array-from-an-existing-array – SeraphimFoA Aug 15 '14 at 14:06
  • Do you have a question? "it didn't work" is a useless problem statement. What did you expect to happen? What happened instead? Compile error? Runtime exception? What is the error/exception and where is it happening? Please put some effort into your question and add some more details so that we can help you. – tnw Aug 15 '14 at 14:06
  • @entropic - depends how you're planning on iterating through the array. For the sake of readability it might make sense to use a foreach so you couldn't start at position 1 (well, you could but you'd need extra code)/ There could be many iterations of the array so it would be a pain to remember to start from position one each time. The array may need to be returned from the function so returning the first line may not make sense. Plenty of reasons IMO. – DoctorMick Aug 15 '14 at 14:10
  • in c# arrays are a fixed length, you simply cannot remove an element. You can however a) ignore the 1st element b) use a List with removeAt method c) create a new array 1 element shorter, and copy accross the elements you want from the original array. In your case i would go with a – Steve Aug 15 '14 at 14:18
  • @DoctorMick Good point. Let's call it a case of tunnel vision on my part. – entropic Aug 15 '14 at 14:23

3 Answers3

3

You could put sr.ReadLine() before sr.ReadToEnd() so the first line would have already have been read and therefore ignored by ReadToEnd.

sr.ReadLine();
string str = sr.ReadToEnd();
DoctorMick
  • 6,703
  • 28
  • 26
2

Just skip the first line

using (StreamReader sr = new StreamReader(stream))
{
    string firstLine = sr.ReadLine();
    // Check to be on the safe side....
    if(firstLine != null)
    {
         string str = sr.ReadToEnd();

         // Not sure, but it seems overkill to use a Regex just to split
         // and it is always better to use predefined constant for newline.
         string[] lines = str.Split(new string[] {Environment.NewLine}, 
                                    StringSplitOptions.RemoveEmptyEntries);
    }
}
Steve
  • 213,761
  • 22
  • 232
  • 286
-1

use Skip, if you're okay with working with IEnumerable rather than a string array.

string[] lines = Regex.Split(str, "\r\n").Skip(1);
Jonesopolis
  • 25,034
  • 12
  • 68
  • 112
  • Skip will return an `IEnumerable`, not an array (and calling `ToArray` will create a _second_ `Array` which is not optimal). – D Stanley Aug 15 '14 at 14:10