DeepSource has joined Harness. Learn more
Java logoJava/
JAVA-P1004

Calling String.indexOf() with a single character string is inefficientJAVA-P1004

Major severityMajor
Performance categoryPerformance

This code passes a single character string, or an empty string to String.indexOf(). Doing so is useless at best, and is also more inefficient than passing a character directly.

Bad Practice

// Use a character or int value instead.
"abc.cat".indexOf(".");

// This will always return index 0.
"abc.cat".indexOf("");

If there is an empty string in the first argument to indexOf, it may indicate that a typo was committed.

It is more efficient to use the integer implementations of indexOf():

myString.indexOf('.')

References