Solved

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

Posted on 2006-07-18
4
15,570 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 63

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 63

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 63

Accepted Solution

by:
Fernando Soto earned 400 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: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

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

Article by: jpaulino
XML Literals are a great way to handle XML files and the community doesn’t use it as much as it should.  An XML Literal is like a String (http://msdn.microsoft.com/en-us/library/system.string.aspx) Literal, only instead of starting and ending with w…
Creating an analog clock UserControl seems fairly straight forward.  It is, after all, essentially just a circle with several lines in it!  Two common approaches for rendering an analog clock typically involve either manually calculating points with…
Monitoring a network: why having a policy is the best policy? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the enormous benefits of having a policy-based approach when monitoring medium and large networks. Software utilized in this v…
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.

627 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