• Post Reply Bookmark Topic Watch Topic
  • New Topic

Search String  RSS feed

 
rudresh kumar
Ranch Hand
Posts: 83
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi All,

I am trying to search string in a ArrayList of strings as we search a database with wildcards.

For example
Suppose Actual String is: - HTML is not enabled
Search query 1: %HTML%
Search query 2 : %TM% is not %nab%
Search query 3 : HTML is *o* (* and % both same (like wild card searches)
Search query 4 : HTML not enabled.

Of the above first 3 should give the result as true except the last.

I had tried the option of matches with regular expression, by adding (?i) at the starting of the string and replacing * or % with .* in the entire string. The problem i am facing now is that i am not able to do a replace of a special character (% or *) with another special character (.*).

I would like to know if there is any other better approach for this and also the solution for replacing a special character with another special character.

Thanks in Advance
 
Rene Larsen
Ranch Hand
Posts: 1179
Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You repalce 'special character' by adding '\\' infront of the character - like this:
 
Rob Spoor
Sheriff
Posts: 21135
87
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Remember to first replace * with .*, then % with .*
If you do it the other way around, % will turn into ..* - since you are replacing its * with .* as well.

I've made that mistake sometimes yes. Quote difficult to debug at times too.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!