سؤال

I have a little problem with RichTextBox, I have a text inside (please look down), and I want remove text line (that's start char'#' and end new line '\n') with using button.

'#BESTPOSA,COM1_30,0,72.5,01*9e9047d2

'#BESTPOSA,COM1,01*6f8c2c77

'$GPGGA,M,06,0126*66

'$GPRMC,152908.00,D*3A

'#AVEPOSA,FINESTEERING,0*eba27375

'$GPGSA,M,1.3*38*

This is a sample from my data.txt (in there are a lot of GPSdata frame). Of course after operation I want have only "$GPxxx" frame. Please help someone.

هل كانت مفيدة؟

المحلول

If you are just extracting, something like:

var lines = richTextBox1.Lines.Where(l => !l.StartsWith("#"));

Will work. If you want to update the RichTextBox then do:

var lines = richTextBox1.Lines.Where(l => !l.StartsWith("#"));
richTextBox1.Text = String.Join("\r\n", lines);

نصائح أخرى

The RichTextBox has a Lines property which will get you a string array with all of the lines in it. You can loop over that and then determine the line length using a helper methods on the RichTextBox.

var lines = richTextBox1.Lines;
for (int i = lines.Count()-1;i>=0; i--)
{
    if (lines[i].StartsWith("#"))
    {
        var thisLineStart = richTextBox1.GetFirstCharIndexFromLine(i);
        var maxLines = richTextBox1.Lines.Count();
        if (i >= maxLines)
        {
            richTextBox1.Text = richTextBox1.Text.Remove(thisLineStart);
        }
        else
        {
            var nextLineStart = richTextBox1.GetFirstCharIndexFromLine(i + 1);
            richTextBox1.Text = richTextBox1.Text.Remove(thisLineStart, nextLineStart - thisLineStart);
        }
    }
}
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top