Solved

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

Posted on 2006-07-18
4
15,384 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
  • 3
4 Comments
 
LVL 62

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 62

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 62

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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Well, all of us have seen the multiple EXCEL.EXE's in task manager that won't die even if you call the .close, .dispose methods. Try this method to kill any excels in memory. You can copy the kill function to create a check function and replace the …
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …

746 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now