Results 1 to 4 of 4
  1. #1
    steve_m is offline Member
    Join Date
    Dec 2010
    Posts
    2
    Rep Power
    0

    Default replaceAll Problem

    Hi,

    I'm trying to replace placeholders in a String (read in from a text file) with actual values.

    The placeholders are in the format of ??name??.

    What I've tried is:

    sk.replaceAll("//?//?title//?//?",it.getItemName());

    where sk is the String I'm replacing in.

    I also tried without any /s and with a single one before each ?. However, none of them seem to make any change to sk (which has many placeholders in that format).

    Could someone show me what I'm doing wrong?

    Thanks,
    Steve

  2. #2
    eRaaaa is offline Senior Member
    Join Date
    Oct 2010
    Location
    Germany
    Posts
    787
    Rep Power
    5

    Default

    Quote Originally Posted by steve_m View Post
    What I've tried is:

    sk.replaceAll("//?//?title//?//?",it.getItemName());

    where sk is the String I'm replacing in.
    Escape with the backslash character ('\') !!

    --> "\\?\\?title\\?\\?"

    However, none of them seem to make any change to sk (which has many placeholders in that format).
    Strings are immutable so you(and the methods :D ) cant change the String sk. The method returns a new String, so you have to write
    sk = sk.replaceAll....

  3. #3
    makpandian's Avatar
    makpandian is offline Senior Member
    Join Date
    Dec 2008
    Location
    Chennai
    Posts
    449
    Rep Power
    6

    Default

    if you are familiar in reg expression, you better use that in replaceAll() method..
    Mak
    (Living @ Virtual World)

  4. #4
    steve_m is offline Member
    Join Date
    Dec 2010
    Posts
    2
    Rep Power
    0

    Default

    Thanks for the help everyone, I'll try that when I get back tonight. Can't believe I've made such glaring mistakes :o

Similar Threads

  1. Regular Expressions and String.replaceAll()
    By meta1203 in forum New To Java
    Replies: 1
    Last Post: 11-24-2010, 11:41 PM
  2. replaceALL(char oldChar, char newChar) method
    By arson09 in forum New To Java
    Replies: 0
    Last Post: 04-28-2010, 05:48 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
  •