StringRules

SUGGESTION

Simplification

View source code on GitHub

Suppression

Disable all rules by adding -XepOpt:Refaster:NamePattern=^(?!StringRules\$).* as compiler argument.

Table of contents
  1. EmptyString
  2. StringIdentity
  3. StringIsEmpty
  4. StringIsEmptyPredicate
  5. StringIsNotEmptyPredicate
  6. StringIsNullOrEmpty
  7. StringIsBlank
  8. OptionalNonEmptyString
  9. FilterEmptyString
  10. JoinStrings
  11. StringJoinDelimiterVarargs
  12. StringValueOf
  13. NewStringFromCharArraySubSequence
  14. NewStringFromCharArray
  15. StringValueOfMethodReference
  16. SubstringRemainder
  17. Utf8EncodedLength
  18. StringIndexOfCharFromIndex
  19. StringIndexOfCharBetweenIndices
  20. StringIndexOfStringFromIndex
  21. StringIndexOfStringBetweenIndices
  22. StringLastIndexOfChar
  23. StringLastIndexOfString
  24. StringLastIndexOfCharWithIndex
  25. StringLastIndexOfStringWithIndex
  26. StringStartsWith
  27. StringFormatted

EmptyString

SUGGESTION

Simplification

Suppression

Suppress false positives by adding the suppression annotation @SuppressWarnings("EmptyString") to the enclosing element.

Disable this rule by adding -XepOpt:Refaster:NamePattern=^(?!StringRules\$EmptyString).* as compiler argument.

Samples

Shows the difference in example code before and after the Refaster rule is applied.

 ImmutableSet<String> testEmptyString() {
-  return ImmutableSet.of(
-      new String(),
-      new String(new byte[0], UTF_8),
-      new String(new byte[] {}, UTF_8),
-      new String(new char[0]),
-      new String(new char[] {}));
+  return ImmutableSet.of("", "", "", "", "");
 }

StringIdentity

SUGGESTION

Simplification

Suppression

Suppress false positives by adding the suppression annotation @SuppressWarnings("StringIdentity") to the enclosing element.

Disable this rule by adding -XepOpt:Refaster:NamePattern=^(?!StringRules\$StringIdentity).* as compiler argument.

Samples

Shows the difference in example code before and after the Refaster rule is applied.

 String testStringIdentity() {
-  return new String("foo");
+  return "foo";
 }

StringIsEmpty

SUGGESTION

Simplification

Suppression

Suppress false positives by adding the suppression annotation @SuppressWarnings("StringIsEmpty") to the enclosing element.

Disable this rule by adding -XepOpt:Refaster:NamePattern=^(?!StringRules\$StringIsEmpty).* as compiler argument.

Samples

Shows the difference in example code before and after the Refaster rule is applied.

 ImmutableSet<Boolean> testStringIsEmpty() {
   return ImmutableSet.of(
-      "foo".length() == 0,
-      "bar".length() <= 0,
-      "baz".length() < 1,
-      "qux".length() != 0,
-      "quux".length() > 0,
-      "corge".length() >= 1);
+      "foo".isEmpty(),
+      "bar".isEmpty(),
+      "baz".isEmpty(),
+      !"qux".isEmpty(),
+      !"quux".isEmpty(),
+      !"corge".isEmpty());
 }

StringIsEmptyPredicate

SUGGESTION

Simplification

Suppression

Suppress false positives by adding the suppression annotation @SuppressWarnings("StringIsEmptyPredicate") to the enclosing element.

Disable this rule by adding -XepOpt:Refaster:NamePattern=^(?!StringRules\$StringIsEmptyPredicate).* as compiler argument.

Samples

Shows the difference in example code before and after the Refaster rule is applied.

 boolean testStringIsEmptyPredicate() {
-  return Stream.of("foo").anyMatch(s -> s.isEmpty());
+  return Stream.of("foo").anyMatch(String::isEmpty);
 }

StringIsNotEmptyPredicate

SUGGESTION

Simplification

Suppression

Suppress false positives by adding the suppression annotation @SuppressWarnings("StringIsNotEmptyPredicate") to the enclosing element.

Disable this rule by adding -XepOpt:Refaster:NamePattern=^(?!StringRules\$StringIsNotEmptyPredicate).* as compiler argument.

Samples

Shows the difference in example code before and after the Refaster rule is applied.

 boolean testStringIsNotEmptyPredicate() {
-  return Stream.of("foo").anyMatch(s -> !s.isEmpty());
+  return Stream.of("foo").anyMatch(not(String::isEmpty));
 }

StringIsNullOrEmpty

SUGGESTION

Simplification

Suppression

Suppress false positives by adding the suppression annotation @SuppressWarnings("StringIsNullOrEmpty") to the enclosing element.

Disable this rule by adding -XepOpt:Refaster:NamePattern=^(?!StringRules\$StringIsNullOrEmpty).* as compiler argument.

Samples

Shows the difference in example code before and after the Refaster rule is applied.

 ImmutableSet<Boolean> testStringIsNullOrEmpty() {
   return ImmutableSet.of(
-      getClass().getName() == null || getClass().getName().isEmpty(),
-      getClass().getName() != null && !getClass().getName().isEmpty());
+      Strings.isNullOrEmpty(getClass().getName()), !Strings.isNullOrEmpty(getClass().getName()));
 }

StringIsBlank

SUGGESTION

Simplification

Suppression

Suppress false positives by adding the suppression annotation @SuppressWarnings("StringIsBlank") to the enclosing element.

Disable this rule by adding -XepOpt:Refaster:NamePattern=^(?!StringRules\$StringIsBlank).* as compiler argument.

Samples

Shows the difference in example code before and after the Refaster rule is applied.

 boolean testStringIsBlank() {
-  return "foo".trim().isEmpty();
+  return "foo".isBlank();
 }

OptionalNonEmptyString

SUGGESTION

Simplification

Suppression

Suppress false positives by adding the suppression annotation @SuppressWarnings("OptionalNonEmptyString") to the enclosing element.

Disable this rule by adding -XepOpt:Refaster:NamePattern=^(?!StringRules\$OptionalNonEmptyString).* as compiler argument.

Samples

Shows the difference in example code before and after the Refaster rule is applied.

 ImmutableSet<Optional<String>> testOptionalNonEmptyString() {
   return ImmutableSet.of(
-      Strings.isNullOrEmpty(toString()) ? Optional.empty() : Optional.of(toString()),
-      Strings.isNullOrEmpty(toString()) ? Optional.empty() : Optional.ofNullable(toString()),
-      !Strings.isNullOrEmpty(toString()) ? Optional.of(toString()) : Optional.empty(),
-      !Strings.isNullOrEmpty(toString()) ? Optional.ofNullable(toString()) : Optional.empty());
+      Optional.ofNullable(toString()).filter(Predicate.not(String::isEmpty)),
+      Optional.ofNullable(toString()).filter(Predicate.not(String::isEmpty)),
+      Optional.ofNullable(toString()).filter(Predicate.not(String::isEmpty)),
+      Optional.ofNullable(toString()).filter(Predicate.not(String::isEmpty)));
 }

FilterEmptyString

SUGGESTION

Simplification

Suppression

Suppress false positives by adding the suppression annotation @SuppressWarnings("FilterEmptyString") to the enclosing element.

Disable this rule by adding -XepOpt:Refaster:NamePattern=^(?!StringRules\$FilterEmptyString).* as compiler argument.

Samples

Shows the difference in example code before and after the Refaster rule is applied.

 Optional<String> testFilterEmptyString() {
-  return Optional.of("foo").map(Strings::emptyToNull);
+  return Optional.of("foo").filter(not(String::isEmpty));
 }

JoinStrings

SUGGESTION

Simplification

Suppression

Suppress false positives by adding the suppression annotation @SuppressWarnings("JoinStrings") to the enclosing element.

Disable this rule by adding -XepOpt:Refaster:NamePattern=^(?!StringRules\$JoinStrings).* as compiler argument.

Samples

Shows the difference in example code before and after the Refaster rule is applied.

 ImmutableSet<String> testJoinStrings() {
   return ImmutableSet.of(
-      Joiner.on("a").join(new String[] {"foo", "bar"}),
-      Joiner.on("b").join(new CharSequence[] {"foo", "bar"}),
-      Arrays.stream(new String[] {"foo", "bar"}).collect(joining("c")),
-      Joiner.on("d").join(ImmutableList.of("foo", "bar")),
-      Streams.stream(Iterables.cycle(ImmutableList.of("foo", "bar"))).collect(joining("e")),
-      ImmutableList.of("foo", "bar").stream().collect(joining("f")));
+      String.join("a", new String[] {"foo", "bar"}),
+      String.join("b", new CharSequence[] {"foo", "bar"}),
+      String.join("c", new String[] {"foo", "bar"}),
+      String.join("d", ImmutableList.of("foo", "bar")),
+      String.join("e", Iterables.cycle(ImmutableList.of("foo", "bar"))),
+      String.join("f", ImmutableList.of("foo", "bar")));
 }

StringJoinDelimiterVarargs

SUGGESTION

Simplification

Suppression

Suppress false positives by adding the suppression annotation @SuppressWarnings("StringJoinDelimiterVarargs") to the enclosing element.

Disable this rule by adding -XepOpt:Refaster:NamePattern=^(?!StringRules\$StringJoinDelimiterVarargs).* as compiler argument.

Samples

Shows the difference in example code before and after the Refaster rule is applied.

 String testStringJoinDelimiterVarargs() {
-  return Stream.of("foo", "bar").collect(joining(","));
+  return String.join(",", "foo", "bar");
 }

StringValueOf

SUGGESTION

Simplification

Suppression

Suppress false positives by adding the suppression annotation @SuppressWarnings("StringValueOf") to the enclosing element.

Disable this rule by adding -XepOpt:Refaster:NamePattern=^(?!StringRules\$StringValueOf).* as compiler argument.

Samples

Shows the difference in example code before and after the Refaster rule is applied.

 String testStringValueOf() {
-  return Objects.toString("foo");
+  return String.valueOf("foo");
 }

NewStringFromCharArraySubSequence

SUGGESTION

Simplification

Suppression

Suppress false positives by adding the suppression annotation @SuppressWarnings("NewStringFromCharArraySubSequence") to the enclosing element.

Disable this rule by adding -XepOpt:Refaster:NamePattern=^(?!StringRules\$NewStringFromCharArraySubSequence).* as compiler argument.

Samples

Shows the difference in example code before and after the Refaster rule is applied.

 ImmutableSet<String> testNewStringFromCharArraySubSequence() {
   return ImmutableSet.of(
-      String.valueOf(new char[] {'f', 'o', 'o'}, 0, 1),
-      String.copyValueOf(new char[] {'b', 'a', 'r'}, 2, 3));
+      new String(new char[] {'f', 'o', 'o'}, 0, 1), new String(new char[] {'b', 'a', 'r'}, 2, 3));
 }

NewStringFromCharArray

SUGGESTION

Simplification

Suppression

Suppress false positives by adding the suppression annotation @SuppressWarnings("NewStringFromCharArray") to the enclosing element.

Disable this rule by adding -XepOpt:Refaster:NamePattern=^(?!StringRules\$NewStringFromCharArray).* as compiler argument.

Samples

Shows the difference in example code before and after the Refaster rule is applied.

 ImmutableSet<String> testNewStringFromCharArray() {
   return ImmutableSet.of(
-      String.valueOf(new char[] {'f', 'o', 'o'}),
-      new String(new char[] {'b', 'a', 'r'}, 0, new char[] {'b', 'a', 'r'}.length));
+      new String(new char[] {'f', 'o', 'o'}), new String(new char[] {'b', 'a', 'r'}));
 }

StringValueOfMethodReference

SUGGESTION

Simplification

Suppression

Suppress false positives by adding the suppression annotation @SuppressWarnings("StringValueOfMethodReference") to the enclosing element.

Disable this rule by adding -XepOpt:Refaster:NamePattern=^(?!StringRules\$StringValueOfMethodReference).* as compiler argument.

Samples

Shows the difference in example code before and after the Refaster rule is applied.

 Function<Object, String> testStringValueOfMethodReference() {
-  return Objects::toString;
+  return String::valueOf;
 }

SubstringRemainder

SUGGESTION

Simplification

Suppression

Suppress false positives by adding the suppression annotation @SuppressWarnings("SubstringRemainder") to the enclosing element.

Disable this rule by adding -XepOpt:Refaster:NamePattern=^(?!StringRules\$SubstringRemainder).* as compiler argument.

Samples

Shows the difference in example code before and after the Refaster rule is applied.

 String testSubstringRemainder() {
-  return "foo".substring(1, "foo".length());
+  return "foo".substring(1);
 }

Utf8EncodedLength

SUGGESTION

Simplification

Suppression

Suppress false positives by adding the suppression annotation @SuppressWarnings("Utf8EncodedLength") to the enclosing element.

Disable this rule by adding -XepOpt:Refaster:NamePattern=^(?!StringRules\$Utf8EncodedLength).* as compiler argument.

Samples

Shows the difference in example code before and after the Refaster rule is applied.

 int testUtf8EncodedLength() {
-  return "foo".getBytes(UTF_8).length;
+  return Utf8.encodedLength("foo");
 }

StringIndexOfCharFromIndex

SUGGESTION

Simplification

Suppression

Suppress false positives by adding the suppression annotation @SuppressWarnings("StringIndexOfCharFromIndex") to the enclosing element.

Disable this rule by adding -XepOpt:Refaster:NamePattern=^(?!StringRules\$StringIndexOfCharFromIndex).* as compiler argument.

Samples

Shows the difference in example code before and after the Refaster rule is applied.

 int testStringIndexOfCharFromIndex() {
-  return "foo".substring(1).indexOf('a');
+  return Math.max(-1, "foo".indexOf('a', 1) - 1);
 }

StringIndexOfCharBetweenIndices

SUGGESTION

Simplification

Suppression

Suppress false positives by adding the suppression annotation @SuppressWarnings("StringIndexOfCharBetweenIndices") to the enclosing element.

Disable this rule by adding -XepOpt:Refaster:NamePattern=^(?!StringRules\$StringIndexOfCharBetweenIndices).* as compiler argument.

Samples

Shows the difference in example code before and after the Refaster rule is applied.

 int testStringIndexOfCharBetweenIndices() {
-  return "foo".substring(1, 2).indexOf('a');
+  return Math.max(-1, "foo".indexOf('a', 1, 2) - 1);
 }

StringIndexOfStringFromIndex

SUGGESTION

Simplification

Suppression

Suppress false positives by adding the suppression annotation @SuppressWarnings("StringIndexOfStringFromIndex") to the enclosing element.

Disable this rule by adding -XepOpt:Refaster:NamePattern=^(?!StringRules\$StringIndexOfStringFromIndex).* as compiler argument.

Samples

Shows the difference in example code before and after the Refaster rule is applied.

 int testStringIndexOfStringFromIndex() {
-  return "foo".substring(1).indexOf("bar");
+  return Math.max(-1, "foo".indexOf("bar", 1) - 1);
 }

StringIndexOfStringBetweenIndices

SUGGESTION

Simplification

Suppression

Suppress false positives by adding the suppression annotation @SuppressWarnings("StringIndexOfStringBetweenIndices") to the enclosing element.

Disable this rule by adding -XepOpt:Refaster:NamePattern=^(?!StringRules\$StringIndexOfStringBetweenIndices).* as compiler argument.

Samples

Shows the difference in example code before and after the Refaster rule is applied.

 int testStringIndexOfStringBetweenIndices() {
-  return "foo".substring(1, 2).indexOf("bar");
+  return Math.max(-1, "foo".indexOf("bar", 1, 2) - 1);
 }

StringLastIndexOfChar

SUGGESTION

Simplification

Suppression

Suppress false positives by adding the suppression annotation @SuppressWarnings("StringLastIndexOfChar") to the enclosing element.

Disable this rule by adding -XepOpt:Refaster:NamePattern=^(?!StringRules\$StringLastIndexOfChar).* as compiler argument.

Samples

Shows the difference in example code before and after the Refaster rule is applied.

 int testStringLastIndexOfChar() {
-  return "foo".substring(1).lastIndexOf('a');
+  return Math.max(-1, "foo".lastIndexOf('a') - 1);
 }

StringLastIndexOfString

SUGGESTION

Simplification

Suppression

Suppress false positives by adding the suppression annotation @SuppressWarnings("StringLastIndexOfString") to the enclosing element.

Disable this rule by adding -XepOpt:Refaster:NamePattern=^(?!StringRules\$StringLastIndexOfString).* as compiler argument.

Samples

Shows the difference in example code before and after the Refaster rule is applied.

 int testStringLastIndexOfString() {
-  return "foo".substring(1).lastIndexOf("bar");
+  return Math.max(-1, "foo".lastIndexOf("bar") - 1);
 }

StringLastIndexOfCharWithIndex

SUGGESTION

Simplification

Suppression

Suppress false positives by adding the suppression annotation @SuppressWarnings("StringLastIndexOfCharWithIndex") to the enclosing element.

Disable this rule by adding -XepOpt:Refaster:NamePattern=^(?!StringRules\$StringLastIndexOfCharWithIndex).* as compiler argument.

Samples

Shows the difference in example code before and after the Refaster rule is applied.

 int testStringLastIndexOfCharWithIndex() {
-  return "foo".substring(0, 2).lastIndexOf('a');
+  return "foo".lastIndexOf('a', 2 - 1);
 }

StringLastIndexOfStringWithIndex

SUGGESTION

Simplification

Suppression

Suppress false positives by adding the suppression annotation @SuppressWarnings("StringLastIndexOfStringWithIndex") to the enclosing element.

Disable this rule by adding -XepOpt:Refaster:NamePattern=^(?!StringRules\$StringLastIndexOfStringWithIndex).* as compiler argument.

Samples

Shows the difference in example code before and after the Refaster rule is applied.

 int testStringLastIndexOfStringWithIndex() {
-  return "foo".substring(0, 2).lastIndexOf("bar");
+  return "foo".lastIndexOf("bar", 2 - 1);
 }

StringStartsWith

SUGGESTION

Simplification

Suppression

Suppress false positives by adding the suppression annotation @SuppressWarnings("StringStartsWith") to the enclosing element.

Disable this rule by adding -XepOpt:Refaster:NamePattern=^(?!StringRules\$StringStartsWith).* as compiler argument.

Samples

Shows the difference in example code before and after the Refaster rule is applied.

 boolean testStringStartsWith() {
-  return "foo".substring(1).startsWith("bar");
+  return "foo".startsWith("bar", 1);
 }

StringFormatted

SUGGESTION

Simplification

Suppression

Suppress false positives by adding the suppression annotation @SuppressWarnings("StringFormatted") to the enclosing element.

Disable this rule by adding -XepOpt:Refaster:NamePattern=^(?!StringRules\$StringFormatted).* as compiler argument.

Samples

Shows the difference in example code before and after the Refaster rule is applied.

 ImmutableSet<String> testStringFormatted() {
   return ImmutableSet.of(
-      String.format("Constant"),
-      String.format("Number: %d", 42),
-      String.format("%s" + "%s", "foo", "bar"));
+      ("Constant").formatted(),
+      ("Number: %d").formatted(42),
+      ("%s" + "%s").formatted("foo", "bar"));
 }

Copyright © 2017-2026 Picnic Technologies BV