Here's a oneliner:
string good = string.Join(Environment.NewLine, mtstring.Split(Environment.NewLine).Reverse().Skip(2).Reverse());
Edit:
I did the following test to test the time efficiency, which shows that (as pointed out by @TheodorZoulias) this approach is quite ineffecient, so use with causion on larger data sets.
class Program
{
static void Main(string[] args)
{
var testString = string.Join(Environment.NewLine, Enumerable.Range(0, 1000000));
var oldArray = testString.Split();
Stopwatch stopwatch = Stopwatch.StartNew();
var newString = string.Join(Environment.NewLine, oldArray);
stopwatch.Stop();
Console.WriteLine($"String length: {newString.Length}");
Console.WriteLine($"Time elapsed: {stopwatch.ElapsedMilliseconds} ms");
stopwatch = Stopwatch.StartNew();
newString = string.Join(Environment.NewLine, oldArray.Reverse().Skip(2).Reverse());
stopwatch.Stop();
Console.WriteLine($"String length: {newString.Length}");
Console.WriteLine($"Time elapsed: {stopwatch.ElapsedMilliseconds} ms");
}
}
Output:
String length: 9888886
Time elapsed: 45 ms
String length: 9888876
Time elapsed: 188 ms
To summarize, the double Reverse
call is quite expensive, quadrupling the execution time compared to just splitting and joining the string, which in itself is no inexpensive task.