Solved

Remove non-ascii chars from a String?

Posted on 2010-09-24
4
810 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Creating Instructional Tutorials  

For Any Use & On Any Platform

Contextual Guidance at the moment of need helps your employees/users adopt software o& achieve even the most complex tasks instantly. Boost knowledge retention, software adoption & employee engagement with easy solution.

Question has a verified solution.

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

Suggested Solutions

Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
Viewers learn about the “while” loop and how to utilize it correctly in Java. Additionally, viewers begin exploring how to include conditional statements within a while loop and avoid an endless loop. Define While Loop: Basic Example: Explanatio…
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
Suggested Courses

739 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