This week's book giveaway is in the OCAJP 8 forum. We're giving away four copies of OCA Java SE 8 Programmer I Study Guide and have Edward Finegan & Robert Liguori on-line! See this thread for details.
I am new to java and I have this query:
Lets say I have the following string : String s = " Dear $1
This is to inform you that $2 is being postponed to $3"
Now simply $1, $2, $3 have to be replaced by some database fields during sending of this message to recipients.
the question is how would my string object s process this string and replace $1,$2,$3 with the appropriate fields from the database? Any clue how to do this?
Do you have to use the $1,$2,$3 convention? Of could you use %s:
This is to inform you that %s has been postponed to %s"
This is to inform you that %2$s has been postponed to %3$s"
Thank you but how %s differs from $1?...I mean is that a format that could be easily replaced? and I don't have to use the conventions I mentioned. I just put them as examples and I wanted to know how could any part of a given string be replaced during whatever execution?
Or you might find that using a MessageFormat (<== follow that link for documentation) would simplify the process. You would again have to use a slightly different format for specifying the parameters. But the point is, use features which already exist in the language rather than inventing your own very similar features and then writing code to implement them.
Joined: Aug 22, 2006
Thank you Paul. The thing is that I had no Idea of such features in java otherwise I wouldn't have posted my Query. However, the hints you dropped are valuable. Thanks again