Solved

how to replace special characters in a String

Posted on 2007-03-21
6
9,335 Views
Last Modified: 2013-12-29
Hi guys

I have a while loop in which I am trying to replace "${"+key+"}" with a value in a String. But somehow it throws a parsing exception. Would anyone know if there is another way to replace special characters from a String.

while(enum.hasMoreElements())
            {
                  String key = (String)enum.nextElement();
                  String value = (String)hshParams.get(key);
                  strSearch = strSearch.replaceAll("${"+key+"}", value);
            }

Anyhelp will be greatly appreciated
0
Comment
Question by:huzefaq
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
6 Comments
 
LVL 86

Accepted Solution

by:
CEHJ earned 300 total points
ID: 18767477
Try

strSearch = strSearch.replaceAll("\\$\\{"+key+"\\}", value);
0
 

Author Comment

by:huzefaq
ID: 18767500
Thanks CEHJ

can you please explain what does the code do. why did you surrounded $ with \\ and also put it after key

Thanks
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 18767532
It's escaping any reserved regex characters
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 92

Assisted Solution

by:objects
objects earned 200 total points
ID: 18767622
$ { and } have special significance in a regex string. So you need to escape them.
You're also going to have problems if your key or value contain any special characters
0
 
LVL 92

Assisted Solution

by:objects
objects earned 200 total points
ID: 18767631
be easier (and a lot more powerful) to use a templating library.
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 18814449
:-)
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.

734 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question