Solved

Remove non-ascii chars from a String?

Posted on 2010-09-24
4
805 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
This video teaches viewers about errors in exception handling.

895 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

14 Experts available now in Live!

Get 1:1 Help Now