开发者

How can I delete the first n lines in a string in C#?

开发者 https://www.devze.com 2023-02-09 03:45 出处:网络
How can I delete the first n lines in a string? Example: String str = @\"a b c d e\"; String output = DeleteLines(str, 2)

How can I delete the first n lines in a string?

Example:

String str = @"a
b
c
d
e";

String output = DeleteLines(str, 2)
//Out开发者_StackOverflow中文版put is "c
//d
//e"


You can use LINQ:

String str = @"a
b
c
d
e";

int n = 2;
string[] lines = str
    .Split(Environment.NewLine.ToCharArray())
    .Skip(n)
    .ToArray();

string output = string.Join(Environment.NewLine, lines);

// Output is 
// "c
// d
// e"


If you need to take into account "\r\n" and "\r" and "\n" it's better to use the following regex:

public static class StringExtensions
{
    public static string RemoveFirstLines(string text, int linesCount)
    {
        var lines = Regex.Split(text, "\r\n|\r|\n").Skip(linesCount);
        return string.Join(Environment.NewLine, lines.ToArray());
    }
}

Here are some more details about splitting text into lines.


Combination of Get the index of the nth occurrence of a string? (search for Environment.NewLine) and substring should do the trick.


Try the following:

public static string DeleteLines(string s, int linesToRemove)
{
    return s.Split(Environment.NewLine.ToCharArray(), 
                   linesToRemove + 1
        ).Skip(linesToRemove)
        .FirstOrDefault();
}

the next example:

string str = @"a
b
c
d
e";
string output = DeleteLines(str, 2);

returns

c
d
e


Try this:

public static string DeleteLines (string text, int lineCount) {
    while (text.Split('\n').Length > lineCount)
        text = text.Remove(0, text.Split('\n')[0].Length + 1);
    return text;
}

It might not be very efficient but it works perfectly for the little project i've been working on recently


Try the following:

    private static string DeleteLines(string input, int lines)
    {
        var result = input;
        for(var i = 0; i < lines; i++)
        {
            var idx = result.IndexOf('\n');
            if (idx < 0)
            {
                // do what you want when there are less than the required lines
                return string.Empty;
            }
            result = result.Substring(idx+1);
        }
        return result;
    }

Note: This method is not ideal for extremely long multi-line strings as it does not consider memory management. If dealing with these kind of strings, I suggest you alter the method to use the StringBuilder class.


With ability to delete first n lines or last n lines:

public static string DeleteLines(
     string stringToRemoveLinesFrom, 
     int numberOfLinesToRemove, 
     bool startFromBottom = false) {
            string toReturn = "";
            string[] allLines = stringToRemoveLinesFrom.Split(
                    separator: Environment.NewLine.ToCharArray(),
                    options: StringSplitOptions.RemoveEmptyEntries);
            if (startFromBottom)
                toReturn = String.Join(Environment.NewLine, allLines.Take(allLines.Length - numberOfLinesToRemove));
            else
                toReturn = String.Join(Environment.NewLine, allLines.Skip(numberOfLinesToRemove));
            return toReturn;
}


public static string DeleteLines(string input, int linesToSkip)
{
    int startIndex = 0;
    for (int i = 0; i < linesToSkip; ++i)
        startIndex = input.IndexOf('\n', startIndex) + 1;
    return input.Substring(startIndex);
}
0

精彩评论

暂无评论...
验证码 换一张
取 消