Class WString
- java.lang.Object
-
- eu.webtoolkit.jwt.WString
-
- All Implemented Interfaces:
java.lang.CharSequence,java.lang.Cloneable,java.lang.Comparable<WString>
public class WString extends java.lang.Object implements java.lang.Comparable<WString>, java.lang.CharSequence, java.lang.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 theWApplication.getLocale().To create a literal string, use
WString(String). To create a localized string you should usetr(String)orWObject.tr(String). The actual value of a string can be obtained usingtoString()orgetValue().A WString can substitute place holders by arguments that are specified using the
arg(CharSequence),arg(double)orarg(int)methods. Place holders are numbered: {n} denotes the n'th place holder.
-
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description WStringappend(char[] buf)Appends a string, converting the string to a literal string if necessary.WStringappend(WString other)Appends a string, converting the string to a literal string if necessary.WStringappend(java.lang.String other)Appends a string, converting the string to a literal string if necessary.WStringarg(double value)Substitutes the next positional argument with a double value.WStringarg(int value)Substitutes the next positional argument with an integer value.WStringarg(java.lang.CharSequence value)Substitutes the next positional argument with a string value.voidchangeArg(int argIndex, double value)Change the WString 's argument at position argIndex.voidchangeArg(int argIndex, int value)Change the WString 's argument at position argIndex.voidchangeArg(int argIndex, java.lang.String value)Change the WString 's argument at position argIndex.charcharAt(int index)Returns the character at a given index.WStringclone()intcompareTo(WString arg0)Compares this WString object with the specified WString object for order.booleanequals(java.lang.Object obj)java.util.List<WString>getArgs()Returns the arguments.java.lang.StringgetJsStringLiteral()Utility method which returns the value as a JavaScript string literal.java.lang.StringgetJsStringLiteral(char delimiter)Utility method which returns the value as a JavaScript string literal.java.lang.StringgetKey()Returns the key for a localized string.intgetLength()Returns the string length.java.lang.StringgetValue()Returns the value.java.lang.StringgetValue(TextFormat format)Returns the value.booleanisEmpty()Returns whether the string is empty.booleanisLiteral()Returns whether the string is literal.intlength()Returns the string length.booleanrefresh()Refreshes the (localized) strings.java.lang.CharSequencesubSequence(int start, int end)Returns a sub string.java.lang.StringtoString()Returns the value of this string.static WStringtoWString(java.lang.CharSequence charSequence)Converts a CharSequence to a WString.java.lang.StringtoXhtml()Returns the XHTML-encoded value of this string.static WStringtr(java.lang.String key)Creates a localized string.static WStringtrn(java.lang.String key, long count)Creates a localized plural string.
-
-
-
Field Detail
-
Empty
public static final WString Empty
Empty WString object.
-
-
Constructor Detail
-
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 Detail
-
clone
public WString clone()
- Overrides:
clonein classjava.lang.Object
-
equals
public boolean equals(java.lang.Object obj)
- Overrides:
equalsin classjava.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:
compareToin interfacejava.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
nullif 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:
toStringin interfacejava.lang.CharSequence- Overrides:
toStringin classjava.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:
charAtin interfacejava.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:
lengthin interfacejava.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:
subSequencein interfacejava.lang.CharSequence
-
-