.NET by Patrik

Remove Extra White Spaces

In C#, you can replace multiple white spaces with a single white space using regular expressions. You can use the Regex class from the System.Text.RegularExpressions namespace to achieve this. Here's an example:

using System;
using System.Text.RegularExpressions;

class Program
{
    static void Main()
    {
        string inputString = "This   is   a    sample    string   with   multiple   spaces.";

        // Use regular expression to replace multiple white spaces with a single white space
        string result = Regex.Replace(inputString, @"\s+", " ");

        Console.WriteLine("Original string: " + inputString);
        Console.WriteLine("Modified string: " + result);
    }
}

In this example, the \s+ regular expression pattern matches one or more white spaces, and the Regex.Replace method replaces these occurrences with a single white space.

Additional Resources

Comments