Exercise – Comparing String.indexOf() and StringUtils.indexOf()
What is the difference between the indexOf()
method of the String
class and the indexOf()
method of the StringUtils
class?
Answer
The indexOf()
method of the String
class does not handle null
. Here is some demo code:
String s = null; int i = StringUtils.indexOf(s, "abc"); //return -1 s.indexOf("abc"); //throws NullPointerException