Class WString

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

public class WString extends Object implements Comparable<WString>, CharSequence, Cloneable
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:
  • Field Details

    • Empty

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

    • WString

      public WString()
      Creates an empty string.
    • WString

      public WString(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

    • clone

      public WString clone()
      Overrides:
      clone in class Object
    • equals

      public boolean equals(Object obj)
      Overrides:
      equals in class 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(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.
      Specified by:
      isEmpty in interface CharSequence
      Returns:
      whether the string is empty.
    • getValue

      public 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 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 Comparable<WString>
    • tr

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

      public static WString trn(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:
    • isLiteral

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

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

      public WString arg(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 List<WString> getArgs()
      Returns the arguments.
      See Also:
    • 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 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 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, 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 String toString()
      Returns the value of this string.
      Specified by:
      toString in interface CharSequence
      Overrides:
      toString in class Object
      See Also:
    • toXhtml

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

      public static WString toWString(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 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 CharSequence
    • getLength

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

      public 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 CharSequence