File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

string processing

 
omar salem
Ranch Hand
Posts: 34
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey ranchers,
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?
 
Steve Luke
Bartender
Pie
Posts: 4181
21
IntelliJ IDE Java Python
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Do you have to use the $1,$2,$3 convention? Of could you use %s:
"Dear %s,
This is to inform you that %s has been postponed to %s"
or:
"Dear %1$s,
This is to inform you that %2$s has been postponed to %3$s"


With these formats you could use the String#format() method. Otherwise, you would probably look into using a String#replace().
 
omar salem
Ranch Hand
Posts: 34
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
Paul Clapham
Sheriff
Pie
Posts: 20171
25
MySQL Database
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
omar salem
Ranch Hand
Posts: 34
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic