Remove non-ascii chars from a String?


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.

Who is Participating?
HegemonConnect With a Mentor Commented:
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.
Gurvinder Pal SinghCommented:
I suppose it depends on how you're defining 'ascii' and what your original encoding
>>it is not an ascii symbol

What is an 'ascii symbol'? Is ' €' one?
All Courses

From novice to tech pro — start learning today.