C#  

Remove spaces and special characters from a string using C#

Step 1: Design Page – RemoveSpacesSpecialChars.aspx

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="RemoveSpacesSpecialChars.aspx.cs" Inherits="RemoveSpacesSpecialChars" %>

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>Remove Spaces and Special Characters - Real Time Example</title>
    <style>
        body {
            font-family: Arial;
            background-color: #f0f2f5;
            margin: 50px;
        }
        .container {
            width: 460px;
            margin: auto;
            background: white;
            border-radius: 8px;
            box-shadow: 0px 0px 10px #ccc;
            padding: 25px;
        }
        h2 {
            color: #1A2A80;
            text-align: center;
        }
        .form-control {
            width: 100%;
            padding: 8px;
            margin-top: 10px;
        }
        .btn {
            background-color: #7A85C1;
            color: white;
            border: none;
            padding: 10px;
            margin-top: 10px;
            border-radius: 5px;
            cursor: pointer;
            width: 100%;
        }
        .result {
            margin-top: 20px;
            font-weight: bold;
            color: #333;
            text-align: center;
            word-wrap: break-word;
        }
    </style>
</head>
<body>
    <form id="form1" runat="server">
        <div class="container">
            <h2>Remove Spaces & Special Characters</h2>

            <asp:Label ID="lblInput" runat="server" Text="Enter a String:"></asp:Label><br />
            <asp:TextBox ID="txtInput" runat="server" CssClass="form-control" placeholder="Example: Hel@lo Wo#rld! 123"></asp:TextBox><br />

            <asp:Button ID="btnRemove" runat="server" Text="Clean String" CssClass="btn" OnClick="btnRemove_Click" /><br />

            <asp:Label ID="lblResult" runat="server" CssClass="result"></asp:Label>
        </div>
    </form>
</body>
</html>

Step 2: Backend Logic – RemoveSpacesSpecialChars.aspx.cs

using System;
using System.Text.RegularExpressions;

public partial class RemoveSpacesSpecialChars : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
    }

    protected void btnRemove_Click(object sender, EventArgs e)
    {
        string input = txtInput.Text.Trim();

        if (string.IsNullOrEmpty(input))
        {
            lblResult.Text = "Please enter a valid string.";
            lblResult.ForeColor = System.Drawing.Color.Red;
            return;
        }

        // Use Regex to remove all non-alphanumeric characters (letters & numbers)
        string cleanedString = Regex.Replace(input, @"[^a-zA-Z0-9]", "");

        lblResult.Text = $"Cleaned String:<br />{cleanedString}";
        lblResult.ForeColor = System.Drawing.Color.Green;
    }
}

Real-Time Example Flow

  1. Open RemoveSpacesSpecialChars.aspx in your browser.

  2. Enter: Hel@lo Wo#rld! 123

  3. Click “Clean String”

  4. Output:

    Cleaned String:
    HelloWorld123
    

Explanation

StepDescription
1Get the input string from user.
2Use Regex.Replace() to remove everything except letters and numbers.
3Display the cleaned result to the user.

Key Concept

Regex Pattern[^a-zA-Z0-9]

  • ^ → NOT these characters

  • a-zA-Z → All letters

  • 0-9 → All numbers
    So the expression means “remove all except alphabets and digits”.

Example Input / Output Table

InputOutput
Hel@lo Wo#rld! 123HelloWorld123
Sandhiya V.#2025SandhiyaV2025
$$C# WebForms$$CWebForms
H@pp*y D@ys!HppyDys
I * CodingICoding