better alternative to message format
Asked Answered
H

3

7

I have a string of following format

Select * where {{0} rdfs:label "Aruba" } limit 10

Now I would like to replace {0} with some new text, but the problem is message format is unable to parse the string due to the first curly bracket. I know if i use '{' it would escape it, but the problem is I have loads of such type of string and I cannot manually add single quotes before and after the curly bracket. Even if I write a function to do this, it would escape the curly brackets for the placeholder {0} as well.

Is their a better alternative to message format, something like ruby string interpolation. I just want a way to write a string template where i can replace certain parts with new string

Hypophosphate answered 13/8, 2011 at 1:20 Comment(2)
right now I have coded like this: NewString=OlString.replace("{0}", "Value");...is this is an inefficient method?Hypophosphate
You're saying you have a string which is not a valid MessageFormat (due to the unescaped { } characters) but which contains MessageFormat style arguments. The answers posted here, including your own, will likely be good enough to address your use-case, but I have to ask how did these malformed strings come to be in the first place? Whatever person or process created them should be expected to provide valid MessageFormat strings (perhaps you should provide a validator); that is a far more robust solution than attempting to modify the strings after the fact.Annulus
F
3

Newer Java versions have java.util.Formatter with its printf like methods. (There are also some variants of them dispersed throughout the API, like String.format and PrintStream.printf).

There you would write

String some_text = "Hello";
String pattern = "Select * where {%s rdfs:label \"Aruba\" } limit 10";
String replaced = String.format(pattern, some_text);
Fare answered 13/8, 2011 at 1:29 Comment(6)
right now I have coded like this: NewString=OlString.replace("{0}", "Value");...do you think this is an inefficient method?Hypophosphate
This depends on the size of your OIString, and how many of these replacements you have. If only one replacement, it will be about the same cost as using MessageFormat or Formatter, I think.Story
In case you want to replace {0} only, this is probably more efficient than using anything smarter. But is efficiency the important thing here?Madura
This is not useful at all. In many cases the printf style is not enough, e.g. when you need to repeat the elements. It can be painful in a long message with repeated elements to use printf. This is not a good answer.Michelinamicheline
@FelipeMicaroniLalli Of course printf is not the solution to all problems ever, but I think it is a valid solution to the problem at hand.Story
I recommend FreeMarker libraryMichelinamicheline
M
1

Replace all direct uses of MessageFormat by your method. In your method, look for the curly braces and replace them based on the context before passing it to MessageFormat. Something as stupid as

s.replace("{", "'{'").replace("}", "'}'").replaceAll("'\\{'(\\d+)'\\}'", "{$1}")

could do, depending on what kinds of arguments you're using.

Don't use this (especially String.replaceAll) in case you're concerned with efficiency. My solution is useful in case you need to preserve the power of MessageFormat. An efficient solution would parse the input string once and recognize which braces should be quoted. Look at the source code of Pattern.replaceAll for how it can be done.

Madura answered 13/8, 2011 at 1:56 Comment(0)
J
1

MessageFormat is just dangerous to use if you don't know exactly what string you are feeding it with. Any presense of a { or } sign would crash whatever you are doing. Here is a small method I wrote just because I needed it immediately. How efficient it is I have not checked. Usually connecting to the DB is 90% of all time spent in answering a request, so if something is efficient functionally, it is usually worth doing. In a jvm-only application, you should be more careful...

public String formatMessage(String pattern, String... replacements ) {

    for(int i = 0; i < replacements.length; i++) {
        pattern = pattern.replace("{" + i + "}", replacements[i]);
    }

    return pattern;
}
Juvenile answered 3/10, 2016 at 8:43 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.