What's the difference between String.matches and Matcher.matches?

后端 未结 5 1780
生来不讨喜
生来不讨喜 2020-12-08 13:11

What\'s the difference between String.matches and Matcher.matches? Is there any difference in terms of performance or other things?

5条回答
  •  轻奢々
    轻奢々 (楼主)
    2020-12-08 13:51

    String.matches internally calls Pattern.matches(regex, str). The problem with it is that each time you call it you recompile the pattern, which cost some resources.

    It is better to compile your pattern once, then try to match it against all the Strings you want. I personally use a Patterns class containing all my pattern in my app declared as final and static

提交回复
热议问题