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

  • SEARCH
  • Home
  • 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 8249819
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T23:40:19+00:00 2026-06-07T23:40:19+00:00

I need to remove characters from a string that aren’t in the Ascii range

  • 0

I need to remove characters from a string that aren’t in the Ascii range from 32 to 175, anything else have to be removed.

I doesn’t known well if RegExp can be the best solution instead of using something like .replace() or .remove() pasing each invalid character or something else.

Any help will be appreciated.

  • 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-06-07T23:40:22+00:00Added an answer on June 7, 2026 at 11:40 pm

    You can use

    Regex.Replace(myString, @"[^\x20-\xaf]+", "");
    

    The regex here consists of a character class ([...]) consisting of all characters not (^ at the start of the class) in the range of U+0020 to U+00AF (32–175, expressed in hexadecimal notation). As far as regular expressions go this one is fairly basic, but may puzzle someone not very familiar with it.

    But you can go another route as well:

    new string(myString.Where(c => (c >= 32) && (c <= 175)).ToArray());
    

    This probably depends mostly on what you’re more comfortable with reading. Without much regex experience I’d say the second one would be clearer.

    A few performance measurements, 10000 rounds each, in seconds:

    2000 characters, the first 143 of which are between 32 and 175
      Regex without +                          4.1171
      Regex with +                             0.4091
      LINQ, where, new string                  0.2176
      LINQ, where, string.Join                 0.2448
      StringBuilder (xanatos)                  0.0355
      LINQ, horrible (HatSoft)                 0.4917
    2000 characters, all of which are between 32 and 175
      Regex without +                          0.4076
      Regex with +                             0.4099
      LINQ, where, new string                  0.3419
      LINQ, where, string.Join                 0.7412
      StringBuilder (xanatos)                  0.0740
      LINQ, horrible (HatSoft)                 0.4801
    

    So yes, my approaches are the slowest :-). You should probably go with xanatos’ answer and wrap that in a method with a nice, clear name. For inline usage or quick-and-dirty things or where performance does not matter, I’d probably use the regex.

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

Sidebar

Related Questions

I have a string of text that I am trying to remove from a
I need to remove all special characters, punctuation and spaces from a string so
I need to remove all characters from the given string except for several which
I need to remove these ' & ' characters from each property of a
I need to remove the following format from the end of a string in
I'm writing a program that has to remove separator characters from quoted strings in
I need to extract some text from a string, then replace that text with
Is there an easy way to remove all non alphanumeric characters from a string
I have a string that I am creating, and I need to add multiple
I have an arbitrary string that comes from a database. I want to place

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.