File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Beginning Java and the fly likes String method Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "String method" Watch "String method" New topic

String method

Qusay Jaafar
Ranch Hand

Joined: May 06, 2002
Posts: 127
1- How can I convert a deprecated String constructor which is:
String(byte[] ascii, int hibyte, int offset, int count)
to another non-deprecated String constructor?
2- How can I convert a deprecated String method which is:
getBytes(int srcBegin, int srcEnd, byte[] dst, int dstBegin)
to another non-deprecated String method?
I appreciate if your explination with a code
Q. Jaafar

Max Habibi
town drunk
( and author)

Joined: Jun 27, 2002
Posts: 4118
1. If this is for the developer certification, then this question should probably be asked in the developer's forum.
2. IMO, this is the sort of thing you should look up. It will only take about five minutes to figure out.

Java Regular Expressions
Jim Yingst

Joined: Jan 30, 2000
Posts: 18671
1. (To Max) Well, maybe. But at this point, it's been asked in a bunch of other forums as well, and others have already chosen to redirect some of the other forums here, so I'd choosing to answer it here for consistency.
(To Quasi) If the value of hibyte used was 0, then the simplest solution is to use the String(byte[], int, int, int) constructor instead:
new String(ascii, offset, count, "ISO-8859-1")
since ISO-8859-1 directly Unicode encoding for values 0-0xFF. Also worth trying is
new String(ascii, offset, count)
since it's reasonably possible the original code simply meant to use the platform default encoding, but didn't get it quite right. This typically makes little difference for "normal" ascii values - but look for funny chars like ™ and ‰ (if they are used anywhere) to see if they're coming out OK or not.
If the value of hibyte is anything other than 0, read the code very carefully and try to determine the author's actual intent. Consider the possibility that they were on crack at the time, and their decoding method may simply be fundamentally wrong. Was there some standard named encoding scheme they were trying to replicate? If so, it might be better to learn the name of the encoding, and use one of the String constructors that accepts a named encoding. If it's really necessary to replicate the original functionality exactly, you can read the description for the deprecated method carefully, and write your own code which creates a char[] array and populates it with chars created from the byte[] array as described (taking hibyte into account). Then use one of the non-deprecated constructors to make a String from the char[].
2. Generally, the answer to this question is probably based on the answer to 1. Whichever constructor/encoding combination turns out to work best above, there's a corresponging getBytes() method which can reverse the process.
[ July 17, 2002: Message edited by: Jim Yingst ]

"I'm not back." - Bill Harding, Twister
I agree. Here's the link:
subject: String method
It's not a secret anymore!