# Use StringUtils Methods

# Properties

Property Value
Rule ID StringUtils
First seen in jSparrow version 1.0.0
Minimum Java version 1.1
Remediation cost 10 min

# Description

This rule replaces various String methods with their null-safe counterparts from StringUtils. For example, "String".trim() becomes StringUtils.trim("String").

Requirements

Activation of this rule requires the following classpath entry to be present:

  • org.apache.commons.lang3.StringUtils

# Benefits

Using StringUtils helps avoid a number of issues, such as null pointer exceptions. Depending on the specific methods readability, performance and others may also be improved. In short: StringUtils is just generally better.

# Implemented Methods

# isEmpty

Pre String API

public boolean isEmpty()

Post StringUtils API

public static boolean isEmpty(CharSequence cs)

# trim

Pre String API

public String trim()

Post StringUtils API

public static String trim(String str)

# equalsIgnoreCase

Pre String API

public boolean equalsIgnoreCase(String anotherString)

Post StringUtils API

public static boolean equalsIgnoreCase(CharSequence str1, CharSequence str2)

# endsWith

Pre String API

public boolean endsWith(String suffix)

Post StringUtils API

public static boolean endsWith(CharSequence str, CharSequence suffix)

# indexOf

Pre String API

public int indexOf(String str)

Post StringUtils API

public static int indexOf(CharSequence seq, CharSequence searchSeq)

# contains

Pre String API

public boolean contains(CharSequence s)

Post StringUtils API

public static boolean contains(CharSequence seq, CharSequence searchSeq)

# substring

Pre String API

public String substring(int beginIndex)

Post StringUtils API

public static String substring(String str, int start)

# startsWith

Pre String API

public boolean startsWith(String prefix)

Post StringUtils API

public static boolean startsWith(CharSequence str, CharSequence prefix)

# toUpperCase

Pre String API

public String toUpperCase()
public String toUpperCase(Locale locale)

Post StringUtils API

public static String upperCase(String str)
public static String upperCase(String str, Locale locale)

# toLowerCase

Pre String API

public String toLowerCase()
public String toLowerCase(Locale locale)

Post StringUtils API

public static String lowerCase(String str)
public static String lowerCase(String str, Locale locale)

# Code Changes

Pre

public boolean testEmpty(String testString) {
    return  /* test */ testString // save me
            .isEmpty() /* trailing comment */;
}

public String testTrim(String testString) {
    String val = /* test */testString.trim();
    return testString.trim();
}

public boolean testEqualsIgnoreCase(String testString) {
    String sometimesExpectedString = testString.replaceAll("a", "b");

    return testString.equalsIgnoreCase(sometimesExpectedString);
}

Post

public boolean testEmpty(String testString) {
    // save me
    return /* test */ StringUtils.isEmpty(testString) /* trailing comment */;
}

public String testTrim(String testString) {
    String val = /* test */StringUtils.trim(testString);
    return StringUtils.trim(testString);
}

public boolean testEqualsIgnoreCase(String testString) {
    String sometimesExpectedString = testString.replaceAll("a", "b");

    return StringUtils.equalsIgnoreCase(testString, sometimesExpectedString);
}

Automatic Application of This Rule

The automatic application of this rule is supported in the following jSparrow version:

# Tags

1
default
You & jSparrow
default

Hey there! May I help you? 😊