Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

how to achieve the "like" effect of SQL in java

 
giang nguyen
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Does any one know how to how to achieve the "like" effect of SQL in java ?
Specifically,how can I compare 2 string like the function like '%anotherString%' in SQL ?
The function returns true if the string is like '%another string%'
Thanks.
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24212
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can use the java.util.regex package in JDK 1.4 to achieve this.
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This particular comparison is also easy to do with the indexOf() method which has been available in String since 1.0:

However the regex package will be far more powerful and flexible for other types of searches you may wish to provide, so it's definitely worth learning about.
 
David Weitzman
Ranch Hand
Posts: 1365
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you just want to do simple wildcard-style matching in a pre-1.4 virtual machine, you can implement a rough matcher without too much code. Here's an example of a function that will match a string against a pattern. Any occurences of the character '*' in the pattern can match 0 or more characters.
 
giang nguyen
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you all for your advices. I've got it done !
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic