Solved

how to replace special characters in a String

Posted on 2007-03-21
6
9,333 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
Space-Age Communications Transitions to DevOps

ViaSat, a global provider of satellite and wireless communications, securely connects businesses, governments, and organizations to the Internet. Learn how ViaSat’s Network Solutions Engineer, drove the transition from a traditional network support to a DevOps-centric model.

 
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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
V.simple question about Android packages and libraries 1 37
HashTable highest marks enumeration alternative 9 48
swing controls 2 21
Java array 10 57
Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
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…
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…

756 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