Class Substring.Match
- All Implemented Interfaces:
CharSequence
- Enclosing class:
Substring
Substring.Pattern against a string, providing access to the
matched substring, to the parts of the string before and
after it, and to copies with the matched substring removed or
replaced.
Note: a Substring.Match is a view of the original string and holds a strong reference
to the original string. It's advisable to construct and use a Match object within the
scope of a method; holding onto a Match object has the same risk of leaking memory as
holding onto the string it was produced from.
- Since:
- 2.0
-
Method Summary
Modifier and TypeMethodDescriptionafter()Returns the part of the original string before the matched substring.before()Returns the part of the original string before the matched substring.charcharAt(int i) booleancontentEquals(String str) Equivalent totoString().equals(str)but without copying the characters into a temporary string.booleanReturns true if this match ends with the givensuffix.Return the full string being matched against.intindex()Return 0-based index of this match infullString().booleanisEmpty()Returns true if the match is empty.booleanisFollowedBy(CharPredicate lookahead) Returns true if the match is immediately followed by a character that matches thelookaheadpredicate.booleanisFollowedBy(String lookahead) Returns true if the match is immediately followed by thelookaheadstring.booleanisImmediatelyBetween(String lookbehind, String lookahead) Returns true if the match immediately follows thelookbehindstring and is immediately followed by thelookaheadstring.booleanReturns true if the match isn't empty.booleanisPrecededBy(CharPredicate lookbehind) Returns true if the match immediately follows a character that matches thelookbehindpredicate.booleanisPrecededBy(String lookbehind) Returns true if the match immediately follows thelookbehindstring.intlength()Returns the length of the matched substring.limit(int maxChars) Returns an equivalent match with at mostmaxChars.remove()Returns a copy of the original string with the matched substring removed.replaceWith(CharSequence replacement) Returns a copy of the original string with the matched substring replaced withreplacement.skip(int fromBeginning, int fromEnd) Returns a new instance that's otherwise equivalent except withfromBeginningcharacters skipped from the beginning andfromEndcharacters skipped from the end.booleanstartsWith(String prefix) Returns true if this match starts with the givenprefix.subSequence(int begin, int end) Returns aCharSequenceinstance which is a sub-range of thisMatch.toString()Returns the matched substring.Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, waitMethods inherited from interface java.lang.CharSequence
chars, codePoints
-
Method Details
-
before
Returns the part of the original string before the matched substring.before()andafter()are almost always used together to split a string into two parts. If you just need the substring before the match, you might want to useSubstring.before(pattern)instead, because the pattern logic is encoded entirely in theSubstring.Patternobject. For example:private static final Substring.Pattern DIRECTORY = Substring.before(last("/")); -
after
Returns the part of the original string before the matched substring.before()andafter()are almost always used together to split a string into two parts. If you just need the substring after the match, you might want to useSubstring.after(pattern)instead, because the pattern logic is encoded entirely in theSubstring.Patternobject. For example:private static final Substring.Pattern LINE_COMMENT = Substring.after(first("//")); -
fullString
Return the full string being matched against. -
remove
Returns a copy of the original string with the matched substring removed.This is equivalent to
match.before() + match.after(). -
replaceWith
Returns a copy of the original string with the matched substring replaced withreplacement.This is equivalent to
match.before() + replacement + match.after(). -
isEmpty
public boolean isEmpty()Returns true if the match is empty.- Specified by:
isEmptyin interfaceCharSequence- Since:
- 7.2
-
isNotEmpty
public boolean isNotEmpty()Returns true if the match isn't empty.- Since:
- 6.0
-
limit
Returns an equivalent match with at mostmaxChars.- Since:
- 6.1
-
skip
Returns a new instance that's otherwise equivalent except withfromBeginningcharacters skipped from the beginning andfromEndcharacters skipped from the end. If there are fewer characters, an empty match is returned.For example,
first("hello").in("say hello").get().skip(2, 1)returns "ll".- Since:
- 6.1
-
index
public int index()Return 0-based index of this match infullString(). -
contentEquals
Equivalent totoString().equals(str)but without copying the characters into a temporary string.- Since:
- 7.1
-
startsWith
Returns true if this match starts with the givenprefix.- Since:
- 7.0
-
endsWith
Returns true if this match ends with the givensuffix.- Since:
- 7.0
-
isFollowedBy
Returns true if the match is immediately followed by thelookaheadstring. Note thatisFollowedBy("")is always true. -
isFollowedBy
Returns true if the match is immediately followed by a character that matches thelookaheadpredicate.- Since:
- 9.0
-
isPrecededBy
Returns true if the match immediately follows thelookbehindstring. Note thatisPrecededBy("")is always true. -
isPrecededBy
Returns true if the match immediately follows a character that matches thelookbehindpredicate.- Since:
- 9.0
-
isImmediatelyBetween
-
length
public int length()Returns the length of the matched substring.- Specified by:
lengthin interfaceCharSequence
-
charAt
public char charAt(int i) - Specified by:
charAtin interfaceCharSequence- Since:
- 4.6
-
subSequence
Returns aCharSequenceinstance which is a sub-range of thisMatch.For example, if this
Matchpoints to the range of"wood"from the"Holywood"string, callingsubSequence(1, 3)will point to the range of"oo"from the original string.Can be used to further reduce the matched range manually.
- Specified by:
subSequencein interfaceCharSequence- Since:
- 4.6
-
toString
Returns the matched substring.- Specified by:
toStringin interfaceCharSequence- Overrides:
toStringin classObject
-