Formatting a string in Java using class attributes
Posted
by Jason R. Coombs
on Stack Overflow
See other posts from Stack Overflow
or by Jason R. Coombs
Published on 2010-03-30T23:59:04Z
Indexed on
2010/03/31
0:03 UTC
Read the original article
Hit count: 326
java
|string-formatting
I have a class with an attribute and getter method:
public Class MyClass
{
private String myValue = "foo";
public String getMyValue();
}
I would like to be able to use the value of foo in a formatted string as such:
String someString = "Your value is {myValue}."
String result = Formatter.format(someString, new MyClass());
// result is now "Your value is foo."
That is, I would like to have some function like .format
above which takes a format string specifying properties on some object, and an instance with those properties, and formats the string accordingly.
Is it possible to do accomplish this feat in Java?
© Stack Overflow or respective owner