How to implement a SQL like 'LIKE' operator in java?

后端 未结 15 1447
你的背包
你的背包 2020-11-29 03:12

I need a comparator in java which has the same semantics as the sql \'like\' operator. For example:

myComparator.like(\"digital\",\"%ital%\");
myComparator.l         


        
15条回答
  •  Happy的楠姐
    2020-11-29 04:01

    You could turn '%string%' to contains(), 'string%' to startsWith() and '%string"' to endsWith().

    You should also run toLowerCase() on both the string and pattern as LIKE is case-insenstive.

    Not sure how you'd handle '%string%other%' except with a Regular Expression though.

    If you're using Regular Expressions:

    • Quote the string before you replace the % characters
    • Watch out for escaped characters in the LIKE String

提交回复
热议问题