Solved

how to replace special characters in a String

Posted on 2007-03-21
6
9,329 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
  • 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
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 
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

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
PDFBox - convert image to PDF, PDF resolution 3 119
Java / Linux and Regular Expressions 11 71
Java Loop 6 50
Oracle SQL syntax check  without executing 6 38
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 …
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.

864 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

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now