Class Replaced

  • All Implemented Interfaces:
    Text

    public final class Replaced
    extends TextEnvelope
    Replace the Text.
    Since:
    0.2
    • Constructor Detail

      • Replaced

        public Replaced​(Text text,
                        CharSequence find,
                        CharSequence replace)
        Ctor.

        Will replace all instances of the substring matched by find with replace.

        Parameters:
        text - The text
        find - The regular expression
        replace - 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 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.

        Parameters:
        text - The text
        regex - The regular expression
        func - Transforms the resulting matcher object into a replacement string. Any exceptions will be wrapped in an IOException.