Results 1 to 5 of 5

Thread: Char replace

  1. #1
    Join Date
    Mar 2011
    Location
    Parma Italy
    Posts
    88
    Rep Power
    0

    Default Char replace

    Hi all,

    I have a string such as String time = "09:03";

    I would like to replace char ':' with empty char, so to print "0903".

    Is there a way to do this using time.replace() or should I use time.substring() method?

    Thanks

    Susanna

  2. #2
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    Have you tried
    Java Code:
    time = time.replace(":", "");
    You can also split the string on ":" and the build the new string with a loop and string builder.

    Just remember, strings are immutable.
    Last edited by sunde887; 06-08-2011 at 12:41 PM.

  3. #3
    Join Date
    Mar 2011
    Location
    Parma Italy
    Posts
    88
    Rep Power
    0

    Default

    Thanks sunde, using time.replace(':',''); it doesn't work but it works using "".

    Susanna

  4. #4
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

  5. #5
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,000
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by susieferrari View Post
    Thanks sunde, using time.replace(':',''); it doesn't work but it works using "".

    Susanna
    ... as you could have read in the API documentation for the String class.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

Similar Threads

  1. concatenate two char into one char
    By nadissen in forum Eclipse
    Replies: 3
    Last Post: 04-06-2011, 02:40 PM
  2. Replies: 2
    Last Post: 03-08-2011, 06:20 AM
  3. check a string char by char
    By Sotsiak in forum New To Java
    Replies: 2
    Last Post: 10-23-2010, 09:24 PM
  4. replaceALL(char oldChar, char newChar) method
    By arson09 in forum New To Java
    Replies: 0
    Last Post: 04-28-2010, 05:48 AM
  5. drawing char by char with Graphics
    By diggitydoggz in forum New To Java
    Replies: 5
    Last Post: 12-27-2008, 12:49 PM

Posting Permissions

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