aspose file tools*
The moose likes Java in General and the fly likes string processing Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "string processing" Watch "string processing" New topic
Author

string processing

omar salem
Ranch Hand

Joined: Aug 22, 2006
Posts: 34
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?

Have a sip of Java !!!
Steve Luke
Bartender

Joined: Jan 28, 2003
Posts: 4181
    
  21

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().


Steve
omar salem
Ranch Hand

Joined: Aug 22, 2006
Posts: 34
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
Bartender

Joined: Oct 14, 2005
Posts: 18882
    
    8

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

Joined: Aug 22, 2006
Posts: 34
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
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: string processing