How to split a string by empty line in C#

1 Answer

0 votes
namespace WinFormsApp1
{
    public partial class Form1 : Form
    {
        string str = "line 1.\r\n\r\nline 2.\r\n\r\nline 3.\r\nline 4.\r\nline 5.";
        public Form1()
        {
            InitializeComponent();
            label1.Text = str;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            string[] lines = str.Split(new[] { Environment.NewLine + Environment.NewLine }, StringSplitOptions.RemoveEmptyEntries);

            label1.Text += Environment.NewLine + Environment.NewLine;

            foreach (string s in lines)
            {
                label1.Text += s;
            }
        }
    }
}




/*
 * run:
 *
 * line 1.
 * 
 * line 2.
 * 
 * line 3.
 * 
 * line 4.
 * 
 * line 5.
 * 
 * line 1.line 2.line 3.
 * line 4.
 * line 5.
 * 
 */


 



answered Jul 31, 2023 by avibootz
edited Jul 31, 2023 by avibootz
...