How would I solve this problem?
I am storing a name and surname in Strings as well as some other information for a user.
The user can give input in form of a string and a output must be delivered in form of a string.
I want to take this for e.g. :
"My name is $name$ and my last name is $surname$, I was born on $date$ and I am now $age$ years old"
The string above is provided by the user (User can place the variables in the order they want them).
I want to convert the variables ($someVar$) that the user provided and turn them into variables that are stored in my program. The variables orders can change, and some may not be used by the user.
The user literally enters $placeholder$ (The user would type out $name$ if they want the name to be returned to them in form of a string)
This is how I decided to solve the problem:
String input = "My name is $name$ and my last name is $surname$, I was born on $date$ and I am now $age$ years old";
input.replace("$name$", "Chris").replace("$surname$", "Smith").replace("$date$", "1962").replace("$age$", "42");
I don't think this is the best method but it should work Can it be improved?
Thanks!