Alex Rivera | Logout

Which String method: "contains" or "indexOf > -1"?

Asked 2012-05-23T06:08:50.390
60

Which of the following ways is an efficient way of determining substring containment?

if (str.indexOf("/") > -1)

or

if (str.contains("/")) 
Edit
Report

1 Answer

7

If the goal is to determine if one String contains another, then contains() is the clear winner. It will make other developers more efficient in understanding your intent.

answered 2012-05-23T06:17:30.793

Your Answer