Class WString

java.lang.Object
eu.webtoolkit.jwt.WString
All Implemented Interfaces:
java.lang.CharSequence, java.lang.Comparable<WString>

public class WString
extends java.lang.Object
implements java.lang.Comparable<WString>, java.lang.CharSequence
A localized string class.

JWt uses WString to store text that may be displayed in the browser.

A WString can either be a literal string or a localized string. A localized string is resolved using WApplication.getLocalizedStrings(), taking into account the WApplication.getLocale().

To create a literal string, use WString(String). To create a localized string you should use tr(String) or WObject.tr(String). The actual value of a string can be obtained using toString() or getValue().

A WString can substitute place holders by arguments that are specified using the arg(CharSequence), arg(double) or arg(int) methods. Place holders are numbered: {n} denotes the n'th place holder.

See Also:
WApplication.getLocalizedStrings(), WApplication.getLocale()
  • Field Summary

    Fields 
    Modifier and Type Field Description
    static WString Empty
    Empty WString object.
  • Constructor Summary

    Constructors 
    Constructor Description
    WString()
    Creates an empty string.
    WString​(char c)
    Creates a literal string from a single character.
    WString​(char[] buf)
    Creates a literal string from a char array.
    WString​(java.lang.String s)
    Creates a literal string.
  • Method Summary

    Modifier and Type Method Description
    WString append​(char[] buf)
    Appends a string, converting the string to a literal string if necessary.
    WString append​(WString other)
    Appends a string, converting the string to a literal string if necessary.
    WString append​(java.lang.String other)
    Appends a string, converting the string to a literal string if necessary.
    WString arg​(double value)
    Substitutes the next positional argument with a double value.
    WString arg​(int value)
    Substitutes the next positional argument with an integer value.
    WString arg​(java.lang.CharSequence value)
    Substitutes the next positional argument with a string value.
    void changeArg​(int argIndex, double value)
    Change the WString 's argument at position argIndex.
    void changeArg​(int argIndex, int value)
    Change the WString 's argument at position argIndex.
    void changeArg​(int argIndex, java.lang.String value)
    Change the WString 's argument at position argIndex.
    char charAt​(int index)
    Returns the character at a given index.
    int compareTo​(WString arg0)
    Compares this WString object with the specified WString object for order.
    boolean equals​(java.lang.Object obj)  
    java.util.List<WString> getArgs()
    Returns the arguments.
    java.lang.String getJsStringLiteral()
    Utility method which returns the value as a JavaScript string literal.
    java.lang.String getJsStringLiteral​(char delimiter)
    Utility method which returns the value as a JavaScript string literal.
    java.lang.String getKey()
    Returns the key for a localized string.
    int getLength()
    Returns the string length.
    java.lang.String getValue()
    Returns the value.
    java.lang.String getValue​(TextFormat format)
    Returns the value.
    boolean isEmpty()
    Returns whether the string is empty.
    boolean isLiteral()
    Returns whether the string is literal.
    int length()
    Returns the string length.
    boolean refresh()
    Refreshes the (localized) strings.
    java.lang.CharSequence subSequence​(int start, int end)
    Returns a sub string.
    java.lang.String toString()
    Returns the value of this string.
    static WString toWString​(java.lang.CharSequence charSequence)
    Converts a CharSequence to a WString.
    java.lang.String toXhtml()
    Returns the XHTML-encoded value of this string.
    static WString tr​(java.lang.String key)
    Creates a localized string.
    static WString trn​(java.lang.String key, long count)
    Creates a localized plural string.

    Methods inherited from class java.lang.Object

    clone, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait

    Methods inherited from interface java.lang.CharSequence

    chars, codePoints
  • Field Details

    • Empty

      public static final WString Empty
      Empty WString object.
  • Constructor Details

    • WString

      public WString()
      Creates an empty string.
    • WString

      public WString​(java.lang.String s)
      Creates a literal string.
    • WString

      public WString​(char c)
      Creates a literal string from a single character.
    • WString

      public WString​(char[] buf)
      Creates a literal string from a char array.
  • Method Details

    • equals

      public boolean equals​(java.lang.Object obj)
      Overrides:
      equals in class java.lang.Object
    • append

      public WString append​(WString other)
      Appends a string, converting the string to a literal string if necessary.
      Returns:
      this
    • append

      public WString append​(java.lang.String other)
      Appends a string, converting the string to a literal string if necessary.
      Returns:
      this
    • append

      public WString append​(char[] buf)
      Appends a string, converting the string to a literal string if necessary.
      Returns:
      this
    • isEmpty

      public boolean isEmpty()
      Returns whether the string is empty.
      Returns:
      whether the string is empty.
    • getValue

      public java.lang.String getValue()
      Returns the value.

      A localized string is resolved using the WApplication.getLocalizedStrings().

      Arguments place holders are substituted with actual arguments.

      Returns:
      the value.
    • getValue

      public java.lang.String getValue​(TextFormat format)
      Returns the value.

      A localized string is resolved using the WApplication.getLocalizedStrings().

      Arguments place holders are substituted with actual arguments.

      Returns:
      the value.
    • compareTo

      public int compareTo​(WString arg0)
      Compares this WString object with the specified WString object for order.
      Specified by:
      compareTo in interface java.lang.Comparable<WString>
    • tr

      public static WString tr​(java.lang.String key)
      Creates a localized string.
      Parameters:
      key - the key which is used to resolve within a locale.
      Returns:
      the localized string.
      See Also:
      WLocalizedStrings.resolveKey(Locale, String)
    • trn

      public static WString trn​(java.lang.String key, long count)
      Creates a localized plural string.
      Parameters:
      key - the key which is used to resolve within a locale.
      count - the count, to retrieve the correct plural form.
      Returns:
      the localized string.
      See Also:
      WLocalizedStrings.resolvePluralKey(Locale, String, long)
    • isLiteral

      public boolean isLiteral()
      Returns whether the string is literal.
      Returns:
      whether the string is literal (versus localized).
    • getKey

      public java.lang.String getKey()
      Returns the key for a localized string.
      Returns:
      the key, or null if the string is literal.
    • arg

      public WString arg​(java.lang.CharSequence value)
      Substitutes the next positional argument with a string value.

      In the string, the n-th argument is referred to as using {n}.

      For example: the string "{1} bought {2} apples in the shop." with first argument value "Bart" and second argument value 5 becomes: "Bart bought 5 apples in the shop."

      Returns:
      this
    • arg

      public WString arg​(int value)
      Substitutes the next positional argument with an integer value.

      In the string, the n-th argument is referred to as using {n}.

      For example: the string "{1} bought {2} apples in the shop." with first argument value "Bart" and second argument value 5 becomes: "Bart bought 5 apples in the shop."

      Returns:
      this
    • arg

      public WString arg​(double value)
      Substitutes the next positional argument with a double value.

      In the string, the n-th argument is referred to as using {n}.

      For example: the string "{1} bought {2} apples in the shop." with first argument value "Bart" and second argument value 5 becomes: "Bart bought 5 apples in the shop."

      Returns:
      this
    • getArgs

      public java.util.List<WString> getArgs()
      Returns the arguments.
      See Also:
      arg(CharSequence)
    • refresh

      public boolean refresh()
      Refreshes the (localized) strings.

      The localized string is resolved again using WApplication.getLocalizedStrings().

      Returns:
      whether the string is (potentially) changed.
    • getJsStringLiteral

      public java.lang.String getJsStringLiteral​(char delimiter)
      Utility method which returns the value as a JavaScript string literal.

      This method escapes the string where needed.

      Parameters:
      delimiter - the quote delimiter to be used ('\'' or '"')
      Returns:
      the quoted string.
    • getJsStringLiteral

      public java.lang.String getJsStringLiteral()
      Utility method which returns the value as a JavaScript string literal.

      Calls getJsStringLiteral('\'')

      Returns:
      the string as a JavaScript string literal.
    • changeArg

      public void changeArg​(int argIndex, java.lang.String value)
      Change the WString 's argument at position argIndex.
    • changeArg

      public void changeArg​(int argIndex, int value)
      Change the WString 's argument at position argIndex.
    • changeArg

      public void changeArg​(int argIndex, double value)
      Change the WString 's argument at position argIndex.
    • toString

      public java.lang.String toString()
      Returns the value of this string.
      Specified by:
      toString in interface java.lang.CharSequence
      Overrides:
      toString in class java.lang.Object
      See Also:
      getValue()
    • toXhtml

      public java.lang.String toXhtml()
      Returns the XHTML-encoded value of this string.
    • toWString

      public static WString toWString​(java.lang.CharSequence charSequence)
      Converts a CharSequence to a WString.

      When the char sequence is a WString, it is casted otherwise, a new WString is created.

      Returns:
      a WString casted or created from the char sequence.
    • charAt

      public char charAt​(int index)
      Returns the character at a given index.

      It is probably more efficient to call getValue() first and then compute on that String.

      Specified by:
      charAt in interface java.lang.CharSequence
    • length

      public int length()
      Returns the string length.

      It is probably more efficient to call getValue() first and then compute on that String.

      Specified by:
      length in interface java.lang.CharSequence
    • getLength

      public int getLength()
      Returns the string length.
      Returns:
      length()
    • subSequence

      public java.lang.CharSequence subSequence​(int start, int end)
      Returns a sub string.

      It is probably more efficient to call getValue() first and then compute on that String.

      Specified by:
      subSequence in interface java.lang.CharSequence