By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
438,553 Members | 1,128 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 438,553 IT Pros & Developers. It's quick & easy.

char to string ????

P: n/a
Is there a method or way for me to take a couple of char's and make a sting
out of them .

Example of what I want to do.

String | char
b = b
ba = a
bad = d
Jul 17 '05 #1
Share this Question
Share on Google+
8 Replies


P: n/a
"Jova" <Na**@dotcom.com> wrote in message news:<FR*********************@news4.srv.hcvlny.cv. net>...
Is there a method or way for me to take a couple of char's and make a sting
out of them .

Example of what I want to do.

String | char
b = b
ba = a
bad = d

couple of char ... char[] couple = {'m', 'f'};
make a string ... String s = new String(couple); // "mf"

Is this what you want?
Jul 17 '05 #2

P: n/a
Yes. Thank You
"hiwa" <HG******@nifty.ne.jp> wrote in message
news:68**************************@posting.google.c om...
"Jova" <Na**@dotcom.com> wrote in message

news:<FR*********************@news4.srv.hcvlny.cv. net>...
Is there a method or way for me to take a couple of char's and make a sting out of them .

Example of what I want to do.

String | char
b = b
ba = a
bad = d

couple of char ... char[] couple = {'m', 'f'};
make a string ... String s = new String(couple); // "mf"

Is this what you want?

Jul 17 '05 #3

P: n/a
HG******@nifty.ne.jp (hiwa) wrote in message news:<68**************************@posting.google. com>...
"Jova" <Na**@dotcom.com> wrote in message news:<FR*********************@news4.srv.hcvlny.cv. net>...
Is there a method or way for me to take a couple of char's and make a sting
out of them .

Example of what I want to do.

String | char
b = b
ba = a
bad = d

couple of char ... char[] couple = {'m', 'f'};
make a string ... String s = new String(couple); // "mf"

Is this what you want?


Another approach would be, where 'b', 'a' and 'd' are chars:

String s = b + a + d + "";

Generally any concatenation involving a string results in a String.
So if you need to a sum, at the same time, you need to isolate it
in brackets:

String s = b + (1+3) + d + "";

Of course if you already have an array of chars, then Jova's
approach is best.

regards

Andre
Jul 17 '05 #4

P: n/a

"Tarken" <ta****@lycos.co.uk> wrote in message
news:e9**************************@posting.google.c om...
HG******@nifty.ne.jp (hiwa) wrote in message

news:<68**************************@posting.google. com>...
"Jova" <Na**@dotcom.com> wrote in message news:<FR*********************@news4.srv.hcvlny.cv. net>...
Is there a method or way for me to take a couple of char's and make a sting out of them .

Example of what I want to do.

String | char
b = b
ba = a
bad = d

couple of char ... char[] couple = {'m', 'f'};
make a string ... String s = new String(couple); // "mf"

Is this what you want?


Another approach would be, where 'b', 'a' and 'd' are chars:

String s = b + a + d + "";

Generally any concatenation involving a string results in a String.
So if you need to a sum, at the same time, you need to isolate it
in brackets:

String s = b + (1+3) + d + "";


I thought it would evaluate left to right, so String s = b + a + d + "";
would be equivalent to

String s = ((( b + a ) + d ) + ""; which is not what was asked for. If you
put the "" first it should work, though. If b, a, and d are int's that is
what it would do. It might treat char specially.

You can also create a StringBuffer, add the chars one at a time, and then
make a String from it.

-- glen

Jul 17 '05 #5

P: n/a
my book says left to right also

"Glen Herrmannsfeldt" <ga*@ugcs.caltech.edu> wrote in message
news:I6onb.47477$HS4.225638@attbi_s01...

"Tarken" <ta****@lycos.co.uk> wrote in message
news:e9**************************@posting.google.c om...
HG******@nifty.ne.jp (hiwa) wrote in message news:<68**************************@posting.google. com>...
"Jova" <Na**@dotcom.com> wrote in message news:<FR*********************@news4.srv.hcvlny.cv. net>... > Is there a method or way for me to take a couple of char's and make
a sting > out of them .
>
> Example of what I want to do.
>
> String | char
> b = b
> ba = a
> bad = d
couple of char ... char[] couple = {'m', 'f'};
make a string ... String s = new String(couple); // "mf"

Is this what you want?
Another approach would be, where 'b', 'a' and 'd' are chars:

String s = b + a + d + "";

Generally any concatenation involving a string results in a String.
So if you need to a sum, at the same time, you need to isolate it
in brackets:

String s = b + (1+3) + d + "";


I thought it would evaluate left to right, so String s = b + a + d + "";
would be equivalent to

String s = ((( b + a ) + d ) + ""; which is not what was asked for. If

you put the "" first it should work, though. If b, a, and d are int's that is
what it would do. It might treat char specially.

You can also create a StringBuffer, add the chars one at a time, and then
make a String from it.

-- glen

Jul 17 '05 #6

P: n/a

"Phil..." <ry***@ieee.org> wrote in message
news:6xxnb.49857$Fm2.24685@attbi_s04...
my book says left to right also

"Glen Herrmannsfeldt" <ga*@ugcs.caltech.edu> wrote in message


(snip)
I thought it would evaluate left to right, so String s = b + a + d + "";
would be equivalent to

String s = ((( b + a ) + d ) + ""; which is not what was asked for. If
you put the "" first it should work, though. You can also create a StringBuffer, add the chars one at a time, and then make a String from it.


I did test it with char's before "" and it does add the value instead of
concatenating as strings, as left to right says it should.

My favorite was something like:

System.out.println( 2 + "+" +2+" equals " +2+2);

Personally, I think overloading the + operator was a bad idea. Notice that
Java doesn't allow user operator overloading, as users might apply operators
in confusing ways. Then again, the PL/I string concatenation operator, ||,
was already taken.

The awk concatenation operator, nothing at all, might have worked, though.

-- glen
Jul 17 '05 #7

P: n/a
doesn't c++ overload + for strings
"Glen Herrmannsfeldt" <ga*@ugcs.caltech.edu> wrote in message
news:JEBnb.52612$e01.129874@attbi_s02...

"Phil..." <ry***@ieee.org> wrote in message
news:6xxnb.49857$Fm2.24685@attbi_s04...
my book says left to right also

"Glen Herrmannsfeldt" <ga*@ugcs.caltech.edu> wrote in message
(snip)
I thought it would evaluate left to right, so String s = b + a + d + ""; would be equivalent to

String s = ((( b + a ) + d ) + ""; which is not what was asked for. Ifyou put the "" first it should work, though. You can also create a StringBuffer, add the chars one at a time, and then make a String from it.


I did test it with char's before "" and it does add the value instead of
concatenating as strings, as left to right says it should.

My favorite was something like:

System.out.println( 2 + "+" +2+" equals " +2+2);

Personally, I think overloading the + operator was a bad idea. Notice

that Java doesn't allow user operator overloading, as users might apply operators in confusing ways. Then again, the PL/I string concatenation operator, ||, was already taken.

The awk concatenation operator, nothing at all, might have worked, though.

-- glen

Jul 17 '05 #8

P: n/a

"Phil..." <ry***@ieee.org> wrote in message
news:aVBnb.36833$mZ5.184610@attbi_s54...
doesn't c++ overload + for strings


(big snip)

Well, Java tends to borrow from C more than C++.

-- glen
Jul 17 '05 #9

This discussion thread is closed

Replies have been disabled for this discussion.