Friday, 20 November 2015

String with variable parameters

Using string with parameters is very handy in case of Constants.

Java class in focus is: java.text.MessageFormat
import java.text.MessageFormat;

public class Welcome {
public static void main(String[] args) {
System.out.println(MessageFormat.format(Constants.WELCOME, "Mr", "Nikhil"));
System.out.println(MessageFormat.format(Constants.WELCOME, "Mrs", "Tejashree"));
}

/**
* Constants class.
*/
public final class Constants {
private Constants() {
}

/**
* Welcome message.
*/
public static final String WELCOME = "Welcome {0}. {1} to njoshi.com";
}
}

 

Output will be

Welcome Mr. Nikhil to njoshi.com
Welcome Mrs. Tejashree to njoshi.com