| Author |
How to replace values in a string?
|
Ajoo Bar
Greenhorn
Joined: Mar 28, 2011
Posts: 28
|
|
Hi,
I am trying to figure out how to do this
I have these properties in my property file
For example
Message1 = My First Name is &1 and my Last Name is &2
Message2 = &1 I live in &3
&1= John
&2=Font
&3= Florida
In my java code I read these properties from file
String message1 = My First Name is &1 and my Last Name is &2
I want to replace the ampersands with &1 and &2 in the message
The above message will look like-- My First Name is John and my Last Name is Font
And if I read message2 the Message will be John lives in Florida
How do I do that?
Thanks
Ajoo
|
 |
Amit Bhargava
Greenhorn
Joined: May 20, 2011
Posts: 20
|
|
|
Try using the String.replaceAll() method. Refer to the Java API for details.
|
 |
Winston Gutkowski
Bartender
Joined: Mar 17, 2011
Posts: 4901
|
|
Ajoo Bar wrote:I want to replace the ampersands with &1 and &2 in the message
How do I do that?
Well, first you ShowSomeEffort. What have you written so far, and what results are you getting?
The policy here is not to simply hand out ready-made code.
Winston
|
Isn't it funny how there's always time and money enough to do it WRONG?
|
 |
Manoj Kumar Jain
Ranch Hand
Joined: Aug 22, 2008
Posts: 191
|
|
You need not to replace the string, I think you should append the string like
|
Do not wait to strike till the iron is hot; but make it hot by striking....
|
 |
fred rosenberger
lowercase baba
Bartender
Joined: Oct 02, 2003
Posts: 10040
|
|
you can't replace values in a String. Strings are immutable - once they are created, they can never be changed.
HOWEVER, you can use various methods of the String class to create a NEW String that looks like what you want, using the old String as a sort of starting point...
|
Never ascribe to malice that which can be adequately explained by stupidity.
|
 |
Paul Clapham
Bartender
Joined: Oct 14, 2005
Posts: 16487
|
|
|
I wouldn't do that. I would just use the MessageFormat class which is part of the standard Java API and which is designed to do exactly that.
|
 |
 |
|
|
subject: How to replace values in a string?
|
|
|