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.
-
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.char
charAt
(int i) boolean
contentEquals
(String str) Equivalent totoString().equals(str)
but without copying the characters into a temporary string.boolean
Returns true if this match ends with the givensuffix
.Return the full string being matched against.int
index()
Return 0-based index of this match infullString()
.boolean
isEmpty()
Returns true if the match is empty.boolean
isFollowedBy
(String lookahead) Returns true if the match is immediately followed by thelookahead
string.boolean
isImmediatelyBetween
(String lookbehind, String lookahead) Returns true if the match immediately follows thelookbehind
string and is immediately followed by thelookahead
string.boolean
Returns true if the match isn't empty.boolean
isPrecededBy
(String lookbehind) Returns true if the match immediately follows thelookbehind
string.int
length()
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 withfromBeginning
characters skipped from the beginning andfromEnd
characters skipped from the end.boolean
startsWith
(String prefix) Returns true if this match starts with the givenprefix
.subSequence
(int begin, int end) Returns aCharSequence
instance 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, wait
Methods 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.Pattern
object. 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.Pattern
object. 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:
isEmpty
in 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 withfromBeginning
characters skipped from the beginning andfromEnd
characters 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 thelookahead
string. Note thatisFollowedBy("")
is always true. -
isPrecededBy
Returns true if the match immediately follows thelookbehind
string. Note thatisPrecededBy("")
is always true. -
isImmediatelyBetween
Returns true if the match immediately follows thelookbehind
string and is immediately followed by thelookahead
string. Note thatisBetween("", "")
is always true. -
length
public int length()Returns the length of the matched substring.- Specified by:
length
in interfaceCharSequence
-
charAt
public char charAt(int i) - Specified by:
charAt
in interfaceCharSequence
- Since:
- 4.6
-
subSequence
Returns aCharSequence
instance which is a sub-range of thisMatch
.For example, if this
Match
points 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:
subSequence
in interfaceCharSequence
- Since:
- 4.6
-
toString
Returns the matched substring.- Specified by:
toString
in interfaceCharSequence
- Overrides:
toString
in classObject
-