1

In theory simple thing but I can't find the answer.

Let's assume we have the String:

"Today at ? cars yellow: ? cars red: ? cars black ?"

And now I want to create to execute some function X in the following way:

X("Today at ? cars yellow: ? cars red: ? cars black", value1, value 2, value 3, value 4)

to get as result the string:

"Today at 12:00 cars yellow: 123 cars red: 11 cars black 24"

Is there any X function ready to be used from the shelf? If not how to write my own?

Pshemo
  • 122,468
  • 25
  • 185
  • 269
user2707175
  • 1,133
  • 2
  • 17
  • 44

7 Answers7

3

You could easily make one:

static String X(String sentence, String... args) {
   for (String arg : args) {
        sentence = sentence.replaceFirst("\\?", arg);
    }
    return sentence;
}

If you are able to modify what the strings look like and use %s instead of ? you could use String.format() instead.

Kevin DiTraglia
  • 25,746
  • 19
  • 92
  • 138
  • Since when `String.format` uses `{0}`? I thought it was `%[argument_index$][flags][width][.precision]conversion`. Maybe you mean `MessageFormat.format` like in [Fouad's answer](http://stackoverflow.com/a/19645442/1393766). – Pshemo Oct 28 '13 at 21:38
  • That would be the perfect solution if I could call it easily ex. for X(string, long, long, string). Otherwise I have to convert manually args to String first. But still big thank you. – user2707175 Oct 29 '13 at 00:39
2

You can eventually use MessageFormat even if the placeholder is not ? but {x} with x the position of the variable in the input list: http://docs.oracle.com/javase/6/docs/api/java/text/MessageFormat.html

Fouad HAMDI
  • 456
  • 2
  • 4
2

Check out the String.format() docs: String.format() javadoc

aryn.galadar
  • 716
  • 4
  • 13
1
static String x(final String format, final Object... values) {

    String f = format.replace("%", "%%");

    f = f.replace("?", "%s");

    return String.format(f, values);
}
Jonathan Rosenne
  • 2,159
  • 17
  • 27
1

This should work fine, look into the String.format() method. Replace each instance of "?" with "%s" and then call String.format and pass all your arguments.

while(myString.contains("?")){
  myString = myString.subString(0,myString.indexOf("?")) + "%s" + myString.subString(myString.indexOf("?" + 1));
}
String final = String.format(myString, val1, val2, val3);
SpacePrez
  • 1,086
  • 7
  • 15
  • 1
    what if my string already contains % used for other purposes? – user2707175 Oct 29 '13 at 00:40
  • That's a good point. You'd have to sanitize the string first, you could convert % into something else and then back to % after the format. But then you need a special symbol that won't be used. String s = "test?%" s = s.replace("%", "@"); s = s.replace("?","%s"); s = String.format(s, arg, arg, arg); s = s.replace("@","%"); – SpacePrez Oct 29 '13 at 16:34
1

The following solution, inspired by Kevin DiTraglia, I would treat as perfect (you can have different objects and primitive types as arguments, not only Strings).

/** replaces ? with arguments */
public static String format (String text, Object... args) {
       for (Object arg : args) {
            text = text.replaceFirst(SPECIAL_CHAR, arg.toString());
        }
    return text;
}
user2707175
  • 1,133
  • 2
  • 17
  • 44
0

I don't think there exists a method for this in java. But one of the implementations can be this (although its tooooo problem specific)

public static String X(String s,String s1,String s2,String s3,String s4){
    String[] array = s.split("\\?");
    s=array[0]+s1+array[1]+s2+array[2]+s3+array[3]+s4;
    return s;
} 
Vikrant Goel
  • 654
  • 6
  • 20
  • or maybe see [Kevin DiTraglia's solution](http://stackoverflow.com/a/19645502/1521453), its better in case you have an array of arguments. – Vikrant Goel Oct 28 '13 at 21:36