locked
Delete Lines from multiline textbox if it's contain certain string - C# RRS feed

  • Question

  • Hello!

    I have a multiline texbox, and would like delete lines if it's contain some string.

    Example

    Line 1 abababalanlndw

    Line 2 aaaaakkk knlnflef baby efqwf

    Line 3 fwelfnwaofna

    If one of the lines contain the "baby" string, i have to delete this line!

    Can you help me?

    Monday, March 19, 2012 8:01 AM

Answers

  • You can do it in a very elegant way using LINQ:

    textBox1.Lines = textBox1.Lines.Where(line => !line.Contains("baby")).ToArray();

    With this instruction, we keep only the lines that don't contain the word "baby".

    Marco Minerva [MCPD]
    Blog: http://blogs.ugidotnet.org/marcom
    Twitter: @marcominerva

    Monday, March 19, 2012 8:30 AM
  • Try the below function. Just pass the TextBox object and String to find.

    private void RemoveLinesContainingToken(TextBox tb, String Token)
    {
        String newText = String.Empty;
        List<String> lines = tb.Text.Split(new String[] { Environment.NewLine }, StringSplitOptions.RemoveEmptyEntries).ToList();
        lines.RemoveAll(str => str.Contains("baby"));
        lines.ForEach(str => newText += str + Environment.NewLine);
        textBox1.Text = newText;
    }

    Now, suppose you want to delete all lines containing "baby" from a TextBox called textBox1, you can call above method as,

    RemoveLinesContainingToken(textBox1, "baby");

    I hope this helps.

    Please mark this post as answer if it solved your problem. Happy Programming!

    • Marked as answer by Helen Zhou Monday, March 26, 2012 9:43 AM
    Monday, March 19, 2012 8:32 AM
  • Hi poli12,

    Besides, you may use Regular expressions, or using the code as in this thread: remove textbox lines that contain certain string.

    Sincerely,

    Helen Zhou [MSFT]
    MSDN Community Support | Feedback to us

    • Marked as answer by Helen Zhou Monday, March 26, 2012 9:43 AM
    Tuesday, March 20, 2012 3:44 AM

All replies

  • You can do it in a very elegant way using LINQ:

    textBox1.Lines = textBox1.Lines.Where(line => !line.Contains("baby")).ToArray();

    With this instruction, we keep only the lines that don't contain the word "baby".

    Marco Minerva [MCPD]
    Blog: http://blogs.ugidotnet.org/marcom
    Twitter: @marcominerva

    Monday, March 19, 2012 8:30 AM
  • Try the below function. Just pass the TextBox object and String to find.

    private void RemoveLinesContainingToken(TextBox tb, String Token)
    {
        String newText = String.Empty;
        List<String> lines = tb.Text.Split(new String[] { Environment.NewLine }, StringSplitOptions.RemoveEmptyEntries).ToList();
        lines.RemoveAll(str => str.Contains("baby"));
        lines.ForEach(str => newText += str + Environment.NewLine);
        textBox1.Text = newText;
    }

    Now, suppose you want to delete all lines containing "baby" from a TextBox called textBox1, you can call above method as,

    RemoveLinesContainingToken(textBox1, "baby");

    I hope this helps.

    Please mark this post as answer if it solved your problem. Happy Programming!

    • Marked as answer by Helen Zhou Monday, March 26, 2012 9:43 AM
    Monday, March 19, 2012 8:32 AM
  • Hi poli12,

    Besides, you may use Regular expressions, or using the code as in this thread: remove textbox lines that contain certain string.

    Sincerely,

    Helen Zhou [MSFT]
    MSDN Community Support | Feedback to us

    • Marked as answer by Helen Zhou Monday, March 26, 2012 9:43 AM
    Tuesday, March 20, 2012 3:44 AM