aspose file tools*
The moose likes Java in General and the fly likes Replacing values in Strings Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Replacing values in Strings" Watch "Replacing values in Strings" New topic
Author

Replacing values in Strings

Steve Wood
Ranch Hand

Joined: Jan 08, 2003
Posts: 137
Hi guys,
I am writing a piece of software that replaces values in strings dynamically. Basically the way I'm doing it is by putting a place holder in the string to put the dynamic value.
String sText = "Hi <Name>, how's it going?";
where <Name> is the place holder. I use the "<" and ">" symbols to indicate the value is dynamic and basically go through the whole string searching for those place holders and then replace the values appropriately.
So here's my question: is this the best way of doing it?
The trouble with this method is that the <> symbols are therefore not allowed in any text - is there any way around this?
Any suggestions are greatly appreciated.
Cheers,
Steve
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
The trouble with this method is that the <> symbols are therefore not allowed in any text - is there any way around this?
Well, several alternatives come to mind:
  • Use some more unique delimiters around Name, e.g. %%Name%% or <#$%Name%$#>.
  • If people need a < or >, require a special escape sequence such as &lt; &gt; or << >>
  • In your search-and-replace method, don't just look for < or > - look specifically for <Name>, or for < followed by letters followed by >. The latter can be represented by a regular expression such as <\w+>. This may still restrict users some, e.g. they can't include HTML tags, or write <somearbitrarytext> for whatever reason. But maybe that's not a problem.

  • I suspect that whatever strategy you use, the java.util.regex package will be helpful. Hope that helps...


    "I'm not back." - Bill Harding, Twister
    Jeanne Boyarsky
    internet detective
    Marshal

    Joined: May 26, 2003
    Posts: 30309
        
    150

    Steve,
    Take a look at the MessageFormat class. It uses expressions like

    These are probably less likely to come up than the <>. It also handles the substitution for you.
    Also, note that the regexp package Jim mentioned is only available starting java 1.4.


    [Blog] [JavaRanch FAQ] [How To Ask Questions The Smart Way] [Book Promos]
    Blogging on Certs: SCEA Part 1, Part 2 & 3, Core Spring 3, OCAJP, OCPJP beta, TOGAF part 1 and part 2
    Layne Lund
    Ranch Hand

    Joined: Dec 06, 2001
    Posts: 3061
    You have hit on a very common problem when defining a syntax for any language. Java has this problem with string literals because there are some special characters like '\n' and '\r' that use the backslash. The solution to this problem is that a single backslash is represented by '\\'.
    Others have posted suggested solutions, so I won't belabor the subject. I just wanted to point out that this problem isn't unique to your particular situation. If you want more in depth information about the issue, you should google for "escape characters" or something similar. You might also be interested in learning more about the theory behind programming languages in general. There are many texts available on the subject.


    Java API Documentation
    The Java Tutorial
    Steve Wood
    Ranch Hand

    Joined: Jan 08, 2003
    Posts: 137
    Thanks a lot, that's very helpful.
    I wasn't sure if I was missing something - though I think I have been. I haven't tried the regex or the MessageFormat classes, so I'm going to look into that. I've been doing the parsing myself and I would imagine the classes are more efficient.
    Jim, I think you're right to check for the whole expression - I've been parsing for the beginning and then assuming the following text has an end delimiter - not all that wise really.
    Thanks for the advice,
    Cheers,
    Steve
    Steve Wood
    Ranch Hand

    Joined: Jan 08, 2003
    Posts: 137
    Hi guys,
    For some reason, I'm trying to use the regex method to as described by Frank, but now I'm getting and 'invalid escape character' problem when using the \w in the pattern. Here's the code:
    Pattern p = Pattern.compile(IFormulaType.IDENTIFIER_START + "\w\w\w\w\w\w\w\w\w\w\w\w\w\w\w\w-\w\w\w\w" + IFormulaType.IDENTIFIER_END);
    Matcher m = p.matcher("Some formula text here that will be tested for the above...");
    boolean b = m.matches();
    Any help is much appreciated.
    Cheers,
    Steve
    Jim Yingst
    Wanderer
    Sheriff

    Joined: Jan 30, 2000
    Posts: 18671
    Frank?
    Sorry, this is one of the confusing things about Java regex, and also another example of difficulties with escape characters as Layne noted. Regular expression syntax uses \ as an escape character for special character classes, like \w which indicates a word character. However when you write a Java literal, the Java compiler also treats \ as a special escape character - except that it doesn't recognize \w as a valid sequence, so it complains. So, you want to tell the Java compiler to treat this \ as a normal char. To do that, you need another \ to escape it. :roll: In other words, in order for the regular expression parser to see the pattern <\w+>, you need to write a Java lString literal of "<\\w+>". Worse, if you ever want to write a regualr expression that looks for an actual \ character, you need to give it a pattern of \\ (for the regular expression parser) which means the Java compiler needs to see a String literal of "\\\\" (so each "\\" translates to one \, and then the two \'s seen by the regex parser translate to a single \ in the "real world"). Beware that most regex literature doesn't bother showing you the Java literal version - they show you what the compiler sees. (As did I, unfortunately.) With a little practice, you get used to basically doubling every \ you see when you want to make a String literal.
    This raises a good point - when you choose an escape sequence, try to make it something different from any sequences that are already being used in that context. We're stuck with \\\\ in Java because Java's escape sequence and Perl's escape sequence were initially developed somewhat independently, and then later people decided Perl's regexes were really cool, so they imported Perl regex into Java. That's a somewhat oversimplified explanation (both histories go back further than either Perl or Java) but it's close enough for now - the point is, if you have a choice in the matter, try to pick as escape sequence that won't confuse people too much later.
    Also, note that MessageFormat apparently doesn't have any escape sequence if you want to include a literal { as part of your message format. And I don't think there's a way to format a String - just various types of numbers and dates. Which is kind of annoying, since that unncessarily limits the usefulness of the class. Though putting in Strings would've made it harder to write and use the parse() method, so maybe it's for the best...
    Steve Wood
    Ranch Hand

    Joined: Jan 08, 2003
    Posts: 137
    Thanks Bob
    I mean Jim - I had been speaking with a Frank and my mind doesn't multi-thread very well.
    Thanks for the explanation. I think you're right about choosing the coding sequence carefully - as these codes are assigned by the software, I'm going to use non-keyboard ascii characters so it's especially unlikely someone will accidentally confuse the system.
    Thanks a lot for your advice - it is very much appreciated.
    Cheers,
    Steve
    John O'Hanley
    Greenhorn

    Joined: Oct 13, 2002
    Posts: 24
    Here is an example of both static and dynamic replacement of text:
    http://www.javapractices.com/Topic80.cjp
    Here is inof about parsing in Java in general:
    http://www.javapractices.com/Topic87.cjp
     
    Consider Paul's rocket mass heater.
     
    subject: Replacing values in Strings