Package com.google.mu.util
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
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 Summary
Modifier and TypeMethodDescriptionaddToIfAbsent
(String string) Ifstring
has this prefix, return it as-is; otherwise, return it with this prefix prepended.boolean
addToIfAbsent
(StringBuilder builder) Ifbuilder
does not already have this prefix, prepend this prefix to it.char
charAt
(int index) boolean
int
hashCode()
Returns a view ofsource
with this prefix hidden away if present, or else returnssource
as is.boolean
isIn
(CharSequence source) Returns true ifsource
starts with this prefix.int
length()
Returns the length of this prefix.boolean
removeFrom
(StringBuilder builder) Removes this prefix frombuilder
if it starts with the prefix.boolean
replaceFrom
(StringBuilder builder, CharSequence replacement) Replaces this prefix frombuilder
withreplacement
if it starts with the prefix.subSequence
(int start, int end) toString()
Returns this prefix string.Methods inherited from class com.google.mu.util.Substring.Pattern
followedBy, from, immediatelyBetween, immediatelyBetween, in, in, limit, notFollowedBy, notImmediatelyBetween, notPrecededBy, or, peek, precededBy, removeFrom, repeatedly, replaceFrom, replaceFrom, separatedBy, separatedBy, skip, split, splitThenTrim, then, toEnd
Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
Methods inherited from interface java.lang.CharSequence
chars, codePoints, isEmpty
-
Method Details
-
isIn
Returns true ifsource
starts with this prefix.- Since:
- 5.7
-
addToIfAbsent
Ifstring
has this prefix, return it as-is; otherwise, return it with this prefix prepended.- Since:
- 4.8
-
addToIfAbsent
Ifbuilder
does not already have this prefix, prepend this prefix to it.- Returns:
- true if this prefix is prepended
- Since:
- 5.7
-
hideFrom
Returns a view ofsource
with this prefix hidden away if present, or else returnssource
as is.- Since:
- 6.5
-
removeFrom
Removes this prefix frombuilder
if it starts with the prefix.- Returns:
- true if this prefix is removed
- Since:
- 5.7
-
replaceFrom
Replaces this prefix frombuilder
withreplacement
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 interfaceCharSequence
- Since:
- 5.7
-
subSequence
- Specified by:
subSequence
in interfaceCharSequence
- Since:
- 5.7
-
length
public int length()Returns the length of this prefix.- Specified by:
length
in interfaceCharSequence
- Since:
- 5.7
-
hashCode
public int hashCode() -
equals
-
toString
Returns this prefix string.- Specified by:
toString
in interfaceCharSequence
- Overrides:
toString
in classSubstring.Pattern
-