Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Find and Replace (or Convert) Non-Ascii Characters In A String

Posted on 2006-07-18
4
Medium Priority
?
15,626 Views
Last Modified: 2008-07-17
Hi everyone.  I'm definately not a VB.NET or developer pro, but I wanted to know if anyone has a function that removes non-ascii characters from a string/CSV readline.  

I'm trying to find something that finds non-ascii characters and replace them with a space or attempt to convert them to ascii (I don't know if this is possible).
0
Comment
Question by:endrec
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
4 Comments
 
LVL 64

Expert Comment

by:Fernando Soto
ID: 17131074
Hi endrec;

When you say non-ascii characters what do you mean?

For example the ASCII character set defines all characters between &H0 through &HFF, 0 - 255 decimal, but not all are printable. Which characters do you want to replace?

Fernando
0
 
LVL 64

Expert Comment

by:Fernando Soto
ID: 17131543
Hi endrec;

This example code will remove all non printable characters from the input string and replace them with a space character.

Imports System.Text.RegularExpressions

    ' Class level variable
    Private re As New Regex("[\x00-\x1F\x7F-\xFF]+", RegexOptions.Compiled)


    Dim input As String ' String that will be striped of all non printable characters.
    input = re.Replace(input, " ")

Input string should now have only printable characters in it.

Fernando
0
 

Author Comment

by:endrec
ID: 17134214
How would I remove non-standard ASCII characters (e.g. any of those characters in the extended ascii set and any non-printable characters)?
0
 
LVL 64

Accepted Solution

by:
Fernando Soto earned 1600 total points
ID: 17138119
Hi endrec;

The above sample code will do that already. The Regex string pattern, "[\x00-\x1F\x7F-\xFF]+", does the following.

Where:
    [ ... ]        Mark a character class and will match any single character in the class.
    +             Quantifier, matches 1 or more of the previous character
    \x00-\x1F A Hex range of valid characters in the class. This is all characters from the begining of the ASCII set to
                  the 31st character which are all control characters.
    \x7F-\xFF Range of characters which are the ASCII extended character set.

This statement :

    input = re.Replace(input, " ")

Will take the string input and replace any of the characters found in the Regex pattern and replace it with a space character.

Fernando
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

If you're writing a .NET application to connect to an Access .mdb database and use pre-existing queries that require parameters, you've come to the right place! Let's say the pre-existing query(qryCust) in Access takes a Date as a parameter and l…
It’s quite interesting for me as I worked with Excel using vb.net for some time. Here are some topics which I know want to share with others whom this might help. First of all if you are working with Excel then you need to Download the Following …
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…
Have you created a query with information for a calendar? ... and then, abra-cadabra, the calendar is done?! I am going to show you how to make that happen. Visualize your data!  ... really see it To use the code to create a calendar from a q…

722 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question