public final class Replaced extends TextEnvelope
Constructor and Description |
---|
Replaced(Text text,
CharSequence find,
CharSequence replace)
Ctor.
|
Replaced(Text text,
Scalar<Pattern> regex,
Func<? super Matcher,? extends CharSequence> func)
Ctor.
|
public Replaced(Text text, CharSequence find, CharSequence replace)
Will replace all instances of the substring matched by find
with replace
.
text
- The textfind
- The regular expressionreplace
- The replacement stringpublic Replaced(Text text, Scalar<Pattern> regex, Func<? super Matcher,? extends CharSequence> func)
The given regex
is used to produce a
matcher
that will be
transformed by func
into a replacement string to replace each
matching
substring.
Example usage:
final String result = new Replaced(
new TextOf("one two THREE four FIVE six"),
() -> Pattern.compile("[a-z]+"),
matcher -> String.valueOf(matcher.group().length())
).asString(); //will return the string "3 3 THREE 4 FIVE 3"
Note: a PatternSyntaxException
will be thrown if the
regular expression's syntax is invalid.
text
- The textregex
- The regular expressionfunc
- Transforms the resulting matcher object into a replacement
string. Any exceptions will be wrapped in an IOException
.Copyright © 2017–2022 Cactoos. All rights reserved.