How do I work with a String with parameters?

3

The question seems to be simple and naive, but I never had to use it, I have a String and inside it I want to put parameters to be filled later, as in php

$string = "o meu nome é $nome";

or as in c, but in c is to display on the screen, in case I want to work with the String. printf("meu nome é %s", nome);

In java I have already worked with something similar but when working with database connection using PreparedStatement .

My real problem is:
I have a String in this format
https://{paíz}.dominio.com.{paíz}/api/page
At first, the most obvious thing is to do concatenations, but since there are several strings in this format, when I hear future maintenance I think it's a little more work.

    
asked by anonymous 02.01.2016 / 22:35

2 answers

3

The only solutions I see is to use an external class such as Formatter or simply use the static format function of class String . Here is a simple example:

import java.util.Formatter;

public class Test {
    public static void main(String[] args) {
        Formatter f = new Formatter();
        f.format("My name is %s%n", "James");

        String firstName = f.toString();
        String lastName = String.format("My last name is %s%n", "Bond");

        System.out.println(firstName);
        System.out.println(lastName);

        f.close();
    }
}
    
02.01.2016 / 22:56
6

StrSubstitutor

Another suitable option for simple variable overrides is StrSubstitutor " of Apache Commons.

Example:

Map valuesMap = HashMap();
valuesMap.put("animal", "quick brown fox");
valuesMap.put("target", "lazy dog"); 
String templateString = "The ${animal} jumped over the ${target}."; 
StrSubstitutor sub = new StrSubstitutor(valuesMap); 
String resolvedString = sub.replace(templateString); 

The disadvantage of this class is having to use a separate library.

But it is very useful, for example, if you want to let the user enter a parametrized template where you will supply the value of the variables and do not want advanced formatting options .

An example is if you want to let the user enter a path pattern for a rotating log file. It could look like this:

/temp/logs/acessos-${data}.log.${numeroArquivo}

And then the system applies the variables data and numeroArquivo when saving the log.

You can change the prefix and suffix of the variables using another constructor of class StrSubstitutor , so you do not have to be stuck with the ${ and } pattern.

This would be a case closer to what you want in the question.

MessageFormat

In addition to the Formatter or String.format() standard, there is a slightly more advanced option that is even supported to pluralize texts, which is more appropriate if the intention is to parameterize the texts of the application and do internationalization and localization.

This is MessageFormat .

Example:

int planet = 7; 
String event = "a disturbance in the Force"; 
String result = MessageFormat.format( "At {1,time} on {1,date}, there was {2} on planet {0,number,integer}.", 
    planet, new Date(), event); 

Considerations

Note that these classes have specific applications and their use is unnecessary in the simplest and most common use cases.

    
03.01.2016 / 07:16