C# Program to Remove Spaces in a String.

Problem: Given a string str, you need to remove all the whitespace from the given string and return a new string without spaces. Write a C# code to solve this problem of removing all spaces from the given string.

Example:

Input: Hello AlgoLesson
Output: HelloAlgoLesson

Input: Good Morning
Output: GoodMorning

We can solve this problem with multiple approaches and here we are going to discuss three different approaches to solve this.

Approach 1: Using string.Replace Method.

In this approach, we use the built-in string.Replace method to replace spaces with an empty string. We will call the Replace method on the input string and provide two arguments: the first argument is the character or substring to be replaced (in this case, a space), and the second argument is the replacement string (an empty string ""). 

Example C# Code:
// C# code implementation to remove whitespace from string
using System;

public class HelloWorld
{
    public static string RemoveSpacesUsingReplace(string input)
    {
        return input.Replace(" ", "");
    }
    public static void Main(string[] args)
    {
        string str = "Welcome to AlgoLesson";
        string result = RemoveSpacesUsingReplace(str);
        
        Console.WriteLine ("With Whitespace: " + str);
        Console.WriteLine ("Without Whitespace: " + result);
    }
}
Output:
With Whitespace: Welcome to AlgoLesson
Without Whitespace: WelcometoAlgoLesson

Approach 2: Using string.Join Method.

This approach splits the input string into an array of substrings using spaces as separators and then joins the substrings together without spaces. We use the Split method to split the input string into an array of substrings. The new[] { ' ' } argument specifies that we want to split the string at spaces. StringSplitOptions.RemoveEmptyEntries removes any empty substrings resulting from consecutive spaces. We then use string.Join to concatenate the array of substrings into a single string without spaces.

Example C# Code:
// C# code implementation to remove whitespace from string using Join method
using System;

public class AlgoLesson
{
    public static string RemoveSpacesUsingSplitAndJoin(string input)
    {
        string[] words = input.Split(new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
        return string.Join("", words);
    }
    public static void Main(string[] args)
    {
        string str = "Welcome to AlgoLesson";
        string result = RemoveSpacesUsingSplitAndJoin(str);
        
        Console.WriteLine ("With Whitespace: " + str);
        Console.WriteLine ("Without Whitespace: " + result);
    }
}
Output:
With Whitespace: Welcome to AlgoLesson
Without Whitespace: WelcometoAlgoLesson

Approach 3: Using string Builder.

This approach uses a StringBuilder to efficiently remove spaces by appending non-space characters. We initialize a StringBuilder (sb) to build the resulting string. We iterate through each character in the input string. If a character is not a space, we append it to the StringBuilder. After processing all characters, we convert the StringBuilder to a string using the ToString method and return the cleaned string.

Example C# Code:

// C# code implementation to remove whitespace from string using string builder
using System;

public class HelloWorld
{
    public static string RemoveSpacesUsingStringBuilder(string input)
    {
        StringBuilder sb = new StringBuilder();
    
        foreach (char c in input)
        {
            if (c != ' ')
            {
                sb.Append(c);
            }
        }
    
        return sb.ToString();
    }
    public static void Main(string[] args)
    {
        string str = "Welcome to AlgoLesson";
        string result = RemoveSpacesUsingSplitAndJoin(str);
        
        Console.WriteLine ("With Whitespace: " + str);
        Console.WriteLine ("Without Whitespace: " + result);
    }
}
Output:
With Whitespace: Welcome to AlgoLesson
Without Whitespace: WelcometoAlgoLesson

These are four different approaches to removing spaces from a string in C#. You can choose the one that best suits your specific requirements and coding style.

⚡ Please share your valuable feedback and suggestion in the comment section below or you can send us an email on our offical email id ✉ algolesson@gmail.com. You can also support our work by buying a cup of coffee ☕ for us.

Similar Posts

No comments:

Post a Comment


CLOSE ADS
CLOSE ADS