adding chars

Discussion in 'Java' started by Rookie, Oct 16, 2003.

  1. Rookie

    Rookie Guest

    How can I add two char to make a string?

    Thank you
    Rookie, Oct 16, 2003
    #1
    1. Advertising

  2. Check out String.valueOf(char) method
    To produce a String you could also do like this:

    char ch1 = 'a';
    char ch2 = 'b';

    String str = ch1 + ch2 + "";

    ---
    Regards,
    Millian Brave <>

    "Rookie" <> skrev i melding news:...
    > How can I add two char to make a string?
    >
    > Thank you
    >
    >
    Millian Brave, Oct 16, 2003
    #2
    1. Advertising

  3. Millian Brave wrote:
    > Check out String.valueOf(char) method
    > To produce a String you could also do like this:
    >
    > char ch1 = 'a';
    > char ch2 = 'b';
    >
    > String str = ch1 + ch2 + "";


    That's exactly how you DON'T do it, because it will add the chars' numeric values
    and then display that as a String, i.e. it will prodcute "195".
    Michael Borgwardt, Oct 16, 2003
    #3
  4. Rookie

    VisionSet Guest

    "Rookie" <> wrote in message news:...
    > How can I add two char to make a string?
    >


    either:

    new String( new char[] {'h', 'e', 'l', 'l', 'o'} );

    or

    StringBuffer buffer = new StringBuffer();

    buffer.append('a');
    buffer.append('b');

    System.out.println(buffer.toString());

    --
    Mike W
    VisionSet, Oct 16, 2003
    #4
  5. Sorry, I switched the order of the "". If you do a:

    String str = "" + ch1 + ch2... you'll get the result you wanted, "ab" that
    is. But I of course agree, this is a dirty way of doing it (implicit cast).


    "Michael Borgwardt" <> skrev i melding
    news:bmlqat$npgic$-berlin.de...
    > Millian Brave wrote:
    > > Check out String.valueOf(char) method
    > > To produce a String you could also do like this:
    > >
    > > char ch1 = 'a';
    > > char ch2 = 'b';
    > >
    > > String str = ch1 + ch2 + "";

    >
    > That's exactly how you DON'T do it, because it will add the chars' numeric

    values
    > and then display that as a String, i.e. it will prodcute "195".
    >
    Millian Brave, Oct 16, 2003
    #5
  6. Millian Brave <> scribbled the following:
    > Sorry, I switched the order of the "". If you do a:


    > String str = "" + ch1 + ch2... you'll get the result you wanted, "ab" that
    > is. But I of course agree, this is a dirty way of doing it (implicit cast).


    Typically, the proper way of making a char into a String would be the
    String.valueOf() method instead of "" + char, but I don't see how it
    could be used to make *two* chars into *one* String.

    --
    /-- Joona Palaste () ------------- Finland --------\
    \-- http://www.helsinki.fi/~palaste --------------------- rules! --------/
    "Nothing lasts forever - so why not destroy it now?"
    - Quake
    Joona I Palaste, Oct 16, 2003
    #6
  7. Yep, that's what I said (String.valueOf()). About the ""+ch1+ch2.. try it
    out yourself.



    "Joona I Palaste" <> skrev i melding
    news:bmm2h9$f5q$...
    > Millian Brave <> scribbled the following:
    > > Sorry, I switched the order of the "". If you do a:

    >
    > > String str = "" + ch1 + ch2... you'll get the result you wanted, "ab"

    that
    > > is. But I of course agree, this is a dirty way of doing it (implicit

    cast).
    >
    > Typically, the proper way of making a char into a String would be the
    > String.valueOf() method instead of "" + char, but I don't see how it
    > could be used to make *two* chars into *one* String.
    >
    > --
    > /-- Joona Palaste () ------------- Finland --------\
    > \-- http://www.helsinki.fi/~palaste --------------------- rules! --------/
    > "Nothing lasts forever - so why not destroy it now?"
    > - Quake
    Millian Brave, Oct 16, 2003
    #7
  8. Joona I Palaste wrote:
    >>String str = "" + ch1 + ch2... you'll get the result you wanted, "ab" that
    >>is. But I of course agree, this is a dirty way of doing it (implicit cast).

    >
    >
    > Typically, the proper way of making a char into a String would be the
    > String.valueOf() method instead of "" + char, but I don't see how it
    > could be used to make *two* chars into *one* String.


    Well, obviously you'd have to invoke it for each char separately and concatente
    the resulting Strings. But then, using a StringBuffer directly would be faster
    and cleaner, since I believe that's what the "+"-concatenation is translated to.
    Michael Borgwardt, Oct 16, 2003
    #8
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Kenneth
    Replies:
    2
    Views:
    11,178
    cheapterp
    Aug 20, 2007
  2. Rick

    adding chars to a string

    Rick, Oct 23, 2003, in forum: C Programming
    Replies:
    23
    Views:
    1,072
    Irrwahn Grausewitz
    Oct 25, 2003
  3. Kosio

    Floats to chars and chars to floats

    Kosio, Sep 16, 2005, in forum: C Programming
    Replies:
    44
    Views:
    1,246
    Tim Rentsch
    Sep 23, 2005
  4. Hongyu
    Replies:
    9
    Views:
    887
    James Kanze
    Aug 8, 2008
  5. M.Posseth

    receiving ??? chars instead of "special" chars

    M.Posseth, Nov 15, 2004, in forum: ASP .Net Web Services
    Replies:
    3
    Views:
    214
    Dan Rogers
    Nov 16, 2004
Loading...

Share This Page