Class Phrase


  • public final class Phrase
    extends Object
    A fluent API for formatting Strings. Canonical usage:
       CharSequence formatted = Phrase.from("Hi {first_name}, you are {age} years old.")
           .put("first_name", firstName)
           .put("age", age)
           .format();
     
    • Surround keys with curly braces; use two {{ to escape.
    • Keys start with lowercase letters followed by lowercase letters and underscores.
    • Spans are preserved, such as simple HTML tags found in strings.xml.
    • Fails fast on any mismatched keys.
    The constructor parses the original pattern into a doubly-linked list of Phrase.Tokens. These tokens do not modify the original pattern, thus preserving any spans.

    The format() method iterates over the tokens, replacing text as it iterates. The doubly-linked list allows each token to ask its predecessor for the expanded length.