Skip to content
Advertisement

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.

Advertisement

Answer

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

User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement