Here is the code that currently extracts the text from a cell:
private String getString(HSSFCell cell) {
String str = null;
if(cell.getCellType() == HSSFCell.CELL_TYPE_STRING)
try{
str = new String(cell.getStringCellValue().getBytes(), "UTF-8");
}catch(Exception e) {
if(Debug.isOn) Debug.out("Encoding Error: " + e.toString());
}
else if(cell.getCellType() == HSSFCell.CELL_TYPE_NUMERIC)
str = new Double(cell.getNumericCellValue()).toString();
else
str = new String();
return str.trim();
}
Any ideas?
~dnm
<snip>
> str = new String(cell.getStringCellValue().getBytes(), "UTF-8");
That looks pretty dodgy to me - it's fetching something as a string
already, encoding it in whatever the default platform encoding is, and
then assuming that that encoded form is the appropriate UTF-8 form. At
least, that's assuming getStringCellValue returns a string, which seems
likely.
Don't forget that strings in Java are already Unicode - they don't
*have* an encoding, as such.
(Two other quick points - there's rarely a need to use new String() -
just use "" instead, unless you really want a different reference.
Similarly, use String.valueOf(double) instead of creating a new Double
and then calling toString.)
--
Jon Skeet - <sk...@pobox.com>
http://www.pobox.com/~skeet/
If replying to the group, please do not mail me too
Actually from what I can see in the source which comes with j2sdk 1.4
best way is: Double.toString(double).
---
Tomy.
-----------------------
t.pet...@inet.hr
It depends on the exact class library - sometimes Double.toString
(double) will be faster than String.valueOf(double), other times it'll
be the other way round. The difference will be minimal. However, the
good thing about using String.valueOf is that it can be used for *all*
types - if you change the type of the variable, you don't need to
change that line of code.
> However, the
> good thing about using String.valueOf is that it can be used for *all*
> types - if you change the type of the variable, you don't need to
> change that line of code.
Agreed 100% on this one :)
--
Tomy.
-----------------------
t.pet...@inet.hr