Win a copy of Svelte and Sapper in Action this week in the JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Bear Bibeault
  • Junilu Lacar
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • salvin francis
  • Frits Walraven
Bartenders:
  • Scott Selikoff
  • Piet Souris
  • Carey Brown

How to replace values in a string?

 
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Try using the String.replaceAll() method. Refer to the Java API for details.
 
Bartender
Posts: 10777
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
 
Ranch Hand
Posts: 198
Oracle Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You need not to replace the string, I think you should append the string like



 
lowercase baba
Posts: 12893
63
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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...
 
Marshal
Posts: 25818
69
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Be reasonable. You can't destroy everything. Where would you sit? How would you read a tiny ad?
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
    Bookmark Topic Watch Topic
  • New Topic