• Post Reply Bookmark Topic Watch Topic
  • New Topic

String replace from HashMap  RSS feed

 
kritika ram
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • 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.
 
Chetan Parekh
Ranch Hand
Posts: 3640
  • Mark post as helpful
  • send pies
  • 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.

 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • 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 ]
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!