# Use StringUtils Methods

# 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.

# Supported API

# 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)

# Tags

# Code Changes

# Checking for Empty Strings

Pre

value.isEmpty();

Post

StringUtils.isEmpty(value);

# Dropping Surrounding Spaces

Pre

value.trim();

Post

StringUtils.trim(value);

# Comparing Equals

Pre

String email = user.getMail();
String username = user.getUsername();

username.equals(expectedUsername);
email.equalsIgnoreCase(expectedEmail);

Post

String email = user.getMail();
String username = user.getUsername();

StringUtils.equals(username, expectedUsername);
StringUtils.equalsIgnoreCase(email, expectedEmail);

# Finding a Substring

Pre

value.substring(index);

Post

StringUtils.substring(value, index);

# Finding the Index of a Substring

Pre

value.indexOf(sequence);

Post

StringUtils.indexOf(value, sequence);

# Comparing the Suffix

Pre

value.endsWith(suffix);

Post

StringUtils.endsWith(value, suffix);

# Comparing the Prefix

Pre

value.startsWith(prefix);

Post

StringUtils.startsWith(value, prefix);

# Searching for a Sequence

Pre

value.contains(sequence);

Post

StringUtils.contains(value, sequence);

# Changing Capitalization

Pre

String upper = value.toUpperCase();
String lower = value.toLowerCase();

Post

String upper = StringUtils.upperCase(value);
String lower = StringUtils.lowerCase(value);

🛠️ Auto-refactor Available

You can auto refactor this with jSparrow.
Drop this button to your Eclipse IDE workspace to install jSparrow for free:

Drag to your running Eclipse* workspace. *Requires Eclipse Marketplace Client

Need help? Check out our installation guide.

# Properties

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