Recipe Name:
Inefficient use of String.indexOf
Description:
Inefficient use of String.indexOf
Level:
warning
Language:
  • java
Tags:
  • Java basic
  • quality
Documentation

When finding the index of a single character, using the String.indexOf(int ch) method instead of the String.indexOf(String s) method is more efficient.

Before
"SenseiRecipe".indexOf("n");
After
"SenseiRecipe".indexOf('n');
Recipe
id: scw:java:string-indexof
version: 10
metadata:
  name: Inefficient use of String.indexOf
  shortDescription: Inefficient use of String.indexOf
  level: warning
  language: java
  enabled: true
  descriptionFile: descriptions/InefficientuseofString.indexOf.html
  tags: Java basic;quality
search:
  methodcall:
    args:
      1:
        type: java.lang.String
        value:
          containsUntrustedInput: false
          stringified:
            matches: ^.$
    name:
      matches: indexOf|lastIndexOf
    declaration:
      type: java.lang.String
availableFixes:
- name: Use char instead of String
  actions:
  - modifyArguments:
      rewrite:
        1: '''{{{value}}}'''