• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

String replace from HashMap

 
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I know its an old and tried question but i do not understand why this code does not work.

public class testreplace {
public static void main(String args[]){
String key="$re$";
String value = "rec";
String message = "me $re$ now";
message = message.replaceAll(key,value);
System.out.println(newm);
}
}
Iam reading the key and value from a HashMap and i need to replace any occurance of a key with the value in the String.
Please help me out. Thanks in advance.
 
Ranch Hand
Posts: 3640
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I just modified your code, please try it. (I haven't compiled and run as I have JDK 1.3.1 and it doesn;t have String.replaceAll().)

Let me know if it works at your end.

 
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The string replaceAll() methods takes a regular expression and a replacement string -- both of which has special (but different) meanings for the "$" character.

I wish that I could explain it further, but regular expressions is a *very* big topic. And I recommend that when you have some free time, to learn it, as it is incredibly useful.

Anyway, to disable the special meaning...



Henry
[ September 16, 2006: Message edited by: Henry Wong ]
 
reply
    Bookmark Topic Watch Topic
  • New Topic