Wednesday, 19 August 2015

Fastest method to trim all whitespace from Strings in .NET

 Method 1

        static Regex whitespace = new Regex(@"\s+");

        private void btnOK_Click(object sender, EventArgs e)
          
        {
            string str = txt1.Text;
            txt2.Text = whitespace.Replace(str, "");
        }


Method 2


 private void btnOK_Click(object sender, EventArgs e)
                 
 {
            string str = txt1.Text;
            txt2.Text = string.Join("", str.Split(default(string[]), StringSplitOptions.RemoveEmptyEntries));
       
}


No comments:

Post a Comment