Results 1 to 7 of 7
  1. #1
    diskhub is offline Member
    Join Date
    May 2010
    Posts
    25
    Rep Power
    0

    Default String replacement...

    I have been given a string by my lecturer such as the following:
    "abc %d %s %t %h %o"

    she told me that i must write something to replace each of the %s, %t, etc
    with a string "apple", "orange"...

    How can i do that?

  2. #2
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    19

    Default

    Many ways to do it. In String class you can find a method replace() for string replacing. Read the API for find the argument list.

  3. #3
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    19

    Default

    Java Code:
          String str="abc %d %s %t %h %o";
          System.out.println(str.replace( "%d","Apple" ));

  4. #4
    diskhub is offline Member
    Join Date
    May 2010
    Posts
    25
    Rep Power
    0

    Default

    Hi,
    was told that there's formatter class that i can use in Java.
    but formatter class only does the standard, %f, %s, %d....

    Can i add customer format to the formatter like %o, %i....

  5. #5
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    19

    Default

    Then you've to think about the way of replacing stuff. You may keep things in a map and use, if you can. Sine I don't know the exact thing I cannot say exactly which way to go.

    Looking at your last post, I feel that you want to format a string, not replace, even both are quite similar.

  6. #6
    diskhub is offline Member
    Join Date
    May 2010
    Posts
    25
    Rep Power
    0

    Default

    Quote Originally Posted by Eranga View Post
    Then you've to think about the way of replacing stuff. You may keep things in a map and use, if you can. Sine I don't know the exact thing I cannot say exactly which way to go.

    Looking at your last post, I feel that you want to format a string, not replace, even both are quite similar.

    nono. i realise string.replace is very good :D
    :D thanks

  7. #7
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    19

Similar Threads

  1. Replies: 7
    Last Post: 11-03-2009, 02:18 AM
  2. combine string[] into string like perl's join function
    By tekberg in forum Advanced Java
    Replies: 9
    Last Post: 02-23-2009, 01:05 PM
  3. Replies: 5
    Last Post: 01-11-2009, 10:12 AM
  4. Looking for JGroups replacement
    By asynchrony in forum Networking
    Replies: 4
    Last Post: 10-17-2008, 10:54 PM
  5. Replies: 0
    Last Post: 11-20-2007, 04:59 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
  •