Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > Checking whether a string contains only ISO-8859-1 chars

Reply
Thread Tools

Checking whether a string contains only ISO-8859-1 chars

 
 
Jonck
Guest
Posts: n/a
 
      10-20-2004
Hi,
I need to send strings to someone else's servlet. However, these strings
may only contain ISO-8859-1 characters, therefore I need to check
whether the user of my app has not tried to enter any non-ISO-8859-1
characters before I send his/her input on to the servlet. Does anyone
know of an easy way to check whether a string contains only ISO-8859-1
characters?

The only solution I could think of was to use a regular expression where
I enter every ISO-8859-1 character in the matching sequence, but this is
rather clunky and prone to errors.

Thanks for any help, Jonck
 
Reply With Quote
 
 
 
 
Thomas Fritsch
Guest
Posts: n/a
 
      10-20-2004
Jonck schrieb:

> Hi,
> I need to send strings to someone else's servlet. However, these strings
> may only contain ISO-8859-1 characters, therefore I need to check
> whether the user of my app has not tried to enter any non-ISO-8859-1
> characters before I send his/her input on to the servlet. Does anyone
> know of an easy way to check whether a string contains only ISO-8859-1
> characters?
>
> The only solution I could think of was to use a regular expression where
> I enter every ISO-8859-1 character in the matching sequence, but this is
> rather clunky and prone to errors.
>
> Thanks for any help, Jonck


Another solution would be:
convert the String into bytes and the bytes back to a String, and then
compare both Strings:
String s = ...;
byte bytes[] = s.getBytes(s, "ISO-8859-1");
String s2 = new String(bytes, "ISO-8859-1");
if (s2.equals(s))
.... // String s is OK
See also the javadoc of String.

--
"Thomas:Fritsch$ops:de".replace(':','.').replace(' $','@')

 
Reply With Quote
 
 
 
 
Chris Smith
Guest
Posts: n/a
 
      10-20-2004
Jonck wrote:
> I need to send strings to someone else's servlet. However, these strings
> may only contain ISO-8859-1 characters, therefore I need to check
> whether the user of my app has not tried to enter any non-ISO-8859-1
> characters before I send his/her input on to the servlet. Does anyone
> know of an easy way to check whether a string contains only ISO-8859-1
> characters?
>
> The only solution I could think of was to use a regular expression where
> I enter every ISO-8859-1 character in the matching sequence, but this is
> rather clunky and prone to errors.


The easiest way to do this is with the java.nio.charset package:

CharsetEncoder enc = Charset.forName("ISO-8859-1").newEncoder();
if (enc.canEncode(str)) ...;
else ...;

For this particular encoding, you could also take advantage of the fact
that ISO-8859-1 contains exactly the set of unicode character with
ordinals less than 256. So you can write this instead:

boolean canEncode = true;
for (int i = 0; i < str.length(); i++)
{
if (str.charAt(i) >= 256)
{
canEncode = false;
break;
}
}

The only advantage of the second approach is that this would work with
any version of the Java API; even obsolete versions like 1.1. For other
encodings, of course, this doesn't work so well.

--
www.designacourse.com
The Easiest Way to Train Anyone... Anywhere.

Chris Smith - Lead Software Developer/Technical Trainer
MindIQ Corporation
 
Reply With Quote
 
Jonck
Guest
Posts: n/a
 
      10-25-2004
Thomas and Chris, thanks to you both for your suggestions, both of your
solutions work perfectly.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: Detect string has non-ASCII chars without checking each char? Michel Claveau - MVP Python 3 08-22-2010 10:13 PM
Re: Detect string has non-ASCII chars without checking each char? Vlastimil Brom Python 1 08-22-2010 05:40 AM
How to truncate char string fromt beginning and replace chars instring by other chars in C or C++? Hongyu C++ 9 08-08-2008 12:18 PM
Checking whether an array contains a majority elemnt.... Amit Bhatnagar C Programming 9 05-04-2006 12:43 AM
Help: what is the quickest way to find out whether a string contains another string? tuweiwen@gmail.com Java 17 12-06-2005 01:04 PM



Advertisments