Solved

Remove non-ascii chars from a String?

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

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

This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
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…
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

757 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

20 Experts available now in Live!

Get 1:1 Help Now