Class Substring.Prefix

java.lang.Object
com.google.mu.util.Substring.Pattern
com.google.mu.util.Substring.Prefix
All Implemented Interfaces:
CharSequence
Enclosing class:
Substring

public static final class Substring.Prefix extends Substring.Pattern implements CharSequence
An immutable string prefix Pattern with extra utilities such as addToIfAbsent(String), removeFrom(StringBuilder), isIn(CharSequence) etc.

Can usually be declared as a constant to save allocation cost. Because Prefix implements CharSequence, it can be used almost interchangeably as a string. You can:

  • directly prepend a prefix as in HOME_PREFIX + path;
  • or prepend it into a StringBuilder: builder.insert(0, HOME_PREFIX);
  • pass it to any CharSequence-accepting APIs such as CharMatcher.anyOf(...).matchesAnyOf(MY_PREFIX), Substring.first(':').splitThenTrim(MY_PREFIX) etc.
Since:
4.6
  • Method Details

    • isIn

      public boolean isIn(CharSequence source)
      Returns true if source starts with this prefix.
      Since:
      5.7
    • addToIfAbsent

      public String addToIfAbsent(String string)
      If string has this prefix, return it as-is; otherwise, return it with this prefix prepended.
      Since:
      4.8
    • addToIfAbsent

      public boolean addToIfAbsent(StringBuilder builder)
      If builder does not already have this prefix, prepend this prefix to it.
      Returns:
      true if this prefix is prepended
      Since:
      5.7
    • hideFrom

      public CharSequence hideFrom(String source)
      Returns a view of source with this prefix hidden away if present, or else returns source as is.
      Since:
      6.5
    • removeFrom

      public boolean removeFrom(StringBuilder builder)
      Removes this prefix from builder if it starts with the prefix.
      Returns:
      true if this prefix is removed
      Since:
      5.7
    • replaceFrom

      public boolean replaceFrom(StringBuilder builder, CharSequence replacement)
      Replaces this prefix from builder with replacement if it starts with the prefix.
      Returns:
      true if this prefix is replaced
      Since:
      5.7
    • charAt

      public char charAt(int index)
      Specified by:
      charAt in interface CharSequence
      Since:
      5.7
    • subSequence

      public String subSequence(int start, int end)
      Specified by:
      subSequence in interface CharSequence
      Since:
      5.7
    • length

      public int length()
      Returns the length of this prefix.
      Specified by:
      length in interface CharSequence
      Since:
      5.7
    • hashCode

      public int hashCode()
      Overrides:
      hashCode in class Object
    • equals

      public boolean equals(Object obj)
      Overrides:
      equals in class Object
    • toString

      public String toString()
      Returns this prefix string.
      Specified by:
      toString in interface CharSequence
      Overrides:
      toString in class Substring.Pattern