Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7037663
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T01:34:21+00:00 2026-05-28T01:34:21+00:00

How do I strip non alphanumeric characters from a string and loose spaces in

  • 0

How do I strip non alphanumeric characters from a string and loose spaces in C# with Replace?

I want to keep a-z, A-Z, 0-9 and nothing more (not even ” ” spaces).

"Hello there(hello#)".Replace(regex-i-want, "");

should give

"Hellotherehello"

I have tried "Hello there(hello#)".Replace(@"[^A-Za-z0-9 ]", ""); but the spaces remain.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T01:34:21+00:00Added an answer on May 28, 2026 at 1:34 am

    In your regex, you have excluded the spaces from being matched (and you haven’t used Regex.Replace() which I had overlooked completely…):

    result = Regex.Replace("Hello there(hello#)", @"[^A-Za-z0-9]+", "");
    

    should work. The + makes the regex a bit more efficient by matching more than one consecutive non-alphanumeric character at once instead of one by one.

    If you want to keep non-ASCII letters/digits, too, use the following regex:

    @"[^\p{L}\p{N}]+"
    

    which leaves

    BonjourmesélèvesGutenMorgenliebeSchüler
    

    instead of

    BonjourmeslvesGutenMorgenliebeSchler
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to strip all non-alphanumeric characters EXCEPT the hyphen from a string (python).
How can you strip non-ASCII characters from a string? (in C#)
I'm looking for a quick and easy way to strip non-alphanumeric characters from an
Possible Duplicate: Javascript: strip out non-numeric characters from string String matching is headache for
How can you strip non-ASCII characters from a string? I like to know how
I have the following code that should strip all non-alphanumeric characters (excluding hyphens) from
I'd like to take a string, strip it of all non-alphanumeric characters and convert
How to strip off HTML tags from a string using plain JavaScript only, not
I'm using the following regex to strip out non-printing control characters from user input
Writing a file utility to strip out all non-ASCII characters from files. I have

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.