Package org.cactoos.text
Class Replaced
- java.lang.Object
-
- org.cactoos.text.TextEnvelope
-
- org.cactoos.text.Replaced
-
- All Implemented Interfaces:
Text
public final class Replaced extends TextEnvelope
Replace the Text.- Since:
- 0.2
-
-
Constructor Summary
Constructors Constructor Description Replaced(Text text, CharSequence find, CharSequence replace)
Ctor.Replaced(Text text, Scalar<Pattern> regex, Func<? super Matcher,? extends CharSequence> func)
Ctor.
-
-
-
Constructor Detail
-
Replaced
public Replaced(Text text, CharSequence find, CharSequence replace)
Ctor.Will replace all instances of the substring matched by
find
withreplace
.- Parameters:
text
- The textfind
- The regular expressionreplace
- The replacement string
-
Replaced
public Replaced(Text text, Scalar<Pattern> regex, Func<? super Matcher,? extends CharSequence> func)
Ctor.The given
regex
is used to produce amatcher
that will be transformed byfunc
into a replacement string to replace eachmatching
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.- Parameters:
text
- The textregex
- The regular expressionfunc
- Transforms the resulting matcher object into a replacement string. Any exceptions will be wrapped in anIOException
.
-
-