Solved

how to replace special characters in a String

Posted on 2007-03-21
6
9,340 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
Revamp Your Training Process

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action.

 
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

More Than Just A Video Library

Train for your certification. Learn the latest DevOps tools. Grow your skillset to do better work.

At Linux Academy, we release new training modules every week so you'll always be up to date on the latest tech.

Question has a verified solution.

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

This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.
Suggested Courses

628 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