Remove last line from a string

c#, string, winforms

Solution

Another option:

str = str.Remove(str.LastIndexOf(Environment.NewLine));

When the last line is empty or contains only white space, and you need to continue removing lines until a non-white-space line has been removed, you just have to trim the end of the string first before calling `LastIndexOf`:

str = str.Remove(str.TrimEnd().LastIndexOf(Environment.NewLine));

Problem

I have a string that could look like this: ``` line1 line2 line3 line4 ``` and I want to remove the last line (line4). How would I do this? I attempted something like this but it requies that I know how many characters the last line contains: ``` output = output.Remove(output.Length - 1, 1) ```

Original source