Successfully added
.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
Referenced in:
Comments(5)
ThomasBet
1/25/2024 6:07:33 AMThomasBet
1/22/2024 2:11:52 AMThomasBet
1/18/2024 5:16:09 PMNatahox
1/18/2024 5:02:24 AMThomasBet
1/16/2024 9:16:37 AM