Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In Java, which is faster - String.contains("some text") or Regex that looks for same text?

As the title says, just looking for a string to match a client finishing sending data over a socket, so I might be looking for something like {"Message" : "END"} in a JSON string for example. A the most the strings will be a few hundred chars long.

like image 518
mal Avatar asked Jan 29 '15 07:01

mal


People also ask

Which is faster indexOf or contains Java?

So "emulating" a Contains() by using an IndexOf with OrdinalIgnoreCase is 400% faster than the builtin Contains().

Can I use regex in contains Java?

String. contains works with String, period. It doesn't work with regex. It will check whether the exact String specified appear in the current String or not.

How do you check if a string matches a regex in Java?

Java - String matches() Method This method tells whether or not this string matches the given regular expression. An invocation of this method of the form str. matches(regex) yields exactly the same result as the expression Pattern. matches(regex, str).

Is string contains method case sensitive in Java?

Yes, contains is case sensitive. You can use java. util.


1 Answers

They're both fast enough to be over before you know it. Better to go for the one that you can read more easily.

But from forums, blogs contains is faster, but still negligible performance difference

like image 56
Ankur Singhal Avatar answered Sep 28 '22 05:09

Ankur Singhal