Thursday, 19 November 2015

How to remove empty spaces from string using dotnet regex?





Use below method to remove empty spaces:


public static string RemoveEmptySpace(string inputString)
        {
            string ouputString = string.Empty;
            if (!string.IsNullOrEmpty(inputString))
            {
                ouputString = Regex.Replace(inputString, "\\s+", " ");
                ouputString = Regex.Replace(ouputString, "^\\s+|\\s+$", "");
            }
            return ouputString;

        }

No comments:

Post a Comment