getChars problem

Hi all, I'm having an issue extracting the first 11 characters from a 14 character string (read in from a DB2 database) using the getChars method. Here's the code:

String s = (rs.getString("partition_name"));

System.out.println("original string: " + s);

int start = 1;

int end = 11;

char newpart[] =newchar[end - start];

s.getChars(start, end, newpart, 0);

System.out.println("new string: " + newpart);

And this is the output:

original string: TB_1538418432_384

new string: [C@533760a8

The output I'm expecting is:

new string: TB_1538418432_

(i.e. the first 11 characters of a 14 character string).

I'm suspecting this is something to do with a mismatch between a database (DB2) that deals in one type of Unicode characters, and a system (UNIX) that deals with another.

However, if anyone can see a flaw in the code, please let me know - otherwise, if it looks OK and anyone thinks this is something to do with incompatible Unicode types, any advice on how to get around that would be hugely welcomed!

TIA.

[1377 byte] By [speedyvespaa] at [2007-11-27 6:53:39]
# 1
An array of chars isn't the same as a String.The output you're seeing is the default output of the toString method of arrays.Create a String out of the newpart array, and then print that.
paulcwa at 2007-7-12 18:28:28 > top of Java-index,Java Essentials,New To Java...
# 2

> Hi all, I'm having an issue extracting the first 11

> characters from a 14 character string (read in from a

> DB2 database) using the getChars method. Here's the

> code:

>

> String s = (rs.getString("partition_name"));

> System.out.println("original string: " + s);

> int start = 1;

> int end = 11;

> char newpart[] = new char[end - start];

>

> s.getChars(start, end, newpart, 0);

> System.out.println("new string: " +

> newpart);

>

> And this is the output:

>

>original string: TB_1538418432_384

> new string: [C@533760a8

[C@533760a8 is the return value of calling toString() on the char[] containing your chars. Try:System.out.println("new string: " + new String(newpart));

However why you are not using s.substring(0, 11) is lost on me.

dwga at 2007-7-12 18:28:28 > top of Java-index,Java Essentials,New To Java...
# 3

If you want to access the characters in a string, use method charAt:

String s = "hello";

char ch = s.charAt(0); //'h' -- offsets start with 0, not 1.

If you want a substring, use method substring:

String s = "hello";

String t = s.substring(0, 4); //"hell"

Hippolytea at 2007-7-12 18:28:28 > top of Java-index,Java Essentials,New To Java...
# 4
Well, largely because I'm at the foot of the learning mountain that is Java classes! :)But thanks very much, the substring worked perfectly. Now I can go home!Thanks again, the help is much appreciated.
speedyvespaa at 2007-7-12 18:28:28 > top of Java-index,Java Essentials,New To Java...