Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Character Array -> String

0 views
Skip to first unread message

nathan

unread,
Sep 3, 1999, 3:00:00 AM9/3/99
to
How would I create a string from char array so that
the string is shorter than the array?

For example, the following code:

char[] help = new char[20];
help[0] = 'a';
help[1] = 'b';
String me = new String(help);

gives me an string of length 20, while I want a
string of length 2. Do I have to use the string
buffer class and manually copy each element of
the array or is there an easier way?


--
-Nathan (remove the junk for my email)

Paul Hilfinger

unread,
Sep 3, 1999, 3:00:00 AM9/3/99
to

In article <7qnb77$h9d$1...@agate-ether.berkeley.edu>, nat...@liriel.HIP.berkeley.edu (nathan) writes:
> How would I create a string from char array so that
> the string is shorter than the array?

To start with, none of the problems in HW1 require such a conversion.
You can use the type String for everything without ever using char
arrays. Indeed, this is how such tasks are almost invariably done in
Java.

However, if you really must perform this conversion, I suggest that
you look at the constructors for String that are described in the
on-line Java library documentation (under String).

PNH

0 new messages