Solved

Remove non-ascii chars from a String?

Posted on 2010-09-24
4
806 Views
Last Modified: 2012-05-10
Hi,

Is there any java utility for removing non-ascii characters from a string? I think I can loop through each character (charAt()) which will give me its "ascii value" and if it's not in the ascii range, just wipe it. But I'm not sure what charAt() will return when I really have non-ascii characters since I've never run into that situation.

How can I do it?

The reason I need to do it is that I'm using a library which does ellipsiszing of a string, and if it encounters and non-ascii char it seems to crash, so want to clean my strings before passing them in.

Thanks
0
Comment
Question by:DJ_AM_Juicebox
  • 2
4 Comments
 
LVL 40

Expert Comment

by:gurvinder372
ID: 33755679
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 33755912
I suppose it depends on how you're defining 'ascii' and what your original encoding
0
 
LVL 10

Accepted Solution

by:
Hegemon earned 500 total points
ID: 33757726
char is 16 bit = 2 bytes, 0 to 65536
ascii is 8 bit = 1 byte, 0 to 255.

So if your char is > 255, it is not an ascii symbol:

if (charAt(i) > 255) {
  // not an ascii symbol, remove.
}
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 33763351
>>it is not an ascii symbol

What is an 'ascii symbol'? Is ' €' one?
0

Featured Post

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

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

Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.

813 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

18 Experts available now in Live!

Get 1:1 Help Now