Results 1 to 4 of 4
  1. #1
    andy9507 is offline Member
    Join Date
    Apr 2011
    Posts
    9
    Rep Power
    0

    Default Convert Character to String

    Hey, how do I caonvert a character to a String? The toString() method isn't working like this...
    String s = c.toString();

  2. #2
    Jodokus's Avatar
    Jodokus is offline Senior Member
    Join Date
    Jan 2011
    Location
    Amsterdam, the Netherlands
    Posts
    230
    Rep Power
    4

    Default

    String str = (new Character( ch )).toString();
    or (better)
    String str = Character.toString( ch );

    Extra explanation: only objects have a toString(). A character (char) is not an object but a primitive.
    Last edited by Jodokus; 05-20-2011 at 11:34 PM.
    No bug ever had to calculate its fitnessfunction.

  3. #3
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default

    You can use the valueOf() method in the String class. Or take advantage of String concatenation:

    Java Code:
    "" + c;

  4. #4
    andy9507 is offline Member
    Join Date
    Apr 2011
    Posts
    9
    Rep Power
    0

    Default

    Thanks guys, you're right, I just had to wrap it in the Character object, valueOf() also worked.

Similar Threads

  1. Removing last character from a String
    By newToJava3 in forum New To Java
    Replies: 2
    Last Post: 11-28-2010, 12:43 AM
  2. split the String into character...
    By Godfrey125 in forum New To Java
    Replies: 2
    Last Post: 11-23-2010, 05:21 PM
  3. compare String to the character .
    By Bulelakes in forum JDBC
    Replies: 12
    Last Post: 07-28-2010, 11:35 AM
  4. Taking a character off the end of a string.
    By AJArmstron@aol.com in forum New To Java
    Replies: 5
    Last Post: 05-05-2010, 02:56 AM
  5. how to get next character/string
    By doha786 in forum New To Java
    Replies: 3
    Last Post: 03-28-2009, 04:04 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •