Win a copy of Node.js Design Patterns: Design and implement production-grade Node.js applications using proven patterns and techniques this week in the Server-Side JavaScript and NodeJS forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Rob Spoor
  • Tim Cooke
  • Junilu Lacar
Sheriffs:
  • Henry Wong
  • Liutauras Vilda
  • Jeanne Boyarsky
Saloon Keepers:
  • Jesse Silverman
  • Tim Holloway
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Mikalai Zaikin
  • Piet Souris

String.replaceAll function error

 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


Hi All,

I am unable to replace a string with $$$ in my String .


String s="select * from table where lastDate between ### and $$$ "

s=s.replaceAll("###", "'"+ fromDate +"'"); here ### is replaced by fromDate
s=s.replaceAll("$$$", "'"+ toDate +"'"); but $$$ is not replace with toDate but its showing as $$$ 'DD/MM/YYYY'


why this is happening
 
Bartender
Posts: 1166
17
Netbeans IDE Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The first argument to replaceAll() is a regular expression and in a regular expression the $ characters is a Meta characters and means the end of line. To replace a $ you must escape it in the regular expression so that it no longer has it's special meaning and the simplest way is to prefix it by \\ . Note - the \\ is needed rather than just \ because Java uses \ as a Meta character ! Messy but ... .
 
Marshal
Posts: 74059
332
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to the Ranch
Are you trying to pick up the dollar sign with a regular expression? But in regular expressions the dollar sign means the end of the line. You can find out all about it in the Java Tutorials. You might have to escape the dollar signs like this \\$
 
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Richard Tookey wrote:To replace a $ you must escape it in the regular expression so that it no longer has it's special meaning and the simplest way is to prefix it by \\ ...


@Yugan: Or use s = s.replace("$$$", "'"+ toDate +"'") instead.

The only drawback with that method is that you can only use it to replace all occurrences.

Winston
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yugan Singams, welcome to the Ranch.

It looks like you want to use this to replace parameters in an SQL statement. Instead of manually trying to build an SQL statement like this, you should use PreparedStatement instead. This is more efficient (the database can cache the statement, so that it doesn't need to interpret it every time you execute it) and safer (because the JDBC driver will escape special characters, which prevents SQL injection attacks).

 
We don't have time for this. We've gotta save the moon! Or check this out:
Thread Boost feature
https://coderanch.com/t/674455/Thread-Boost-feature
reply
    Bookmark Topic Watch Topic
  • New Topic