Class 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
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.
-
Field Summary
-
Constructor Summary
-
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.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.static WString
tr(java.lang.String key)
Creates a localized 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
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 classjava.lang.Object
-
append
Appends a string, converting the string to a literal string if necessary.- Returns:
this
-
append
Appends a string, converting the string to a literal string if necessary.- Returns:
this
-
append
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 interfacejava.lang.CharSequence
- 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.
-
compareTo
Compares this WString object with the specified WString object for order.- Specified by:
compareTo
in interfacejava.lang.Comparable<WString>
-
tr
Creates a localized string.- Parameters:
key
- the key which is used to resolve within a locale.- Returns:
- the localized string.
- See Also:
WLocalizedStrings.resolveKey(String)
-
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
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
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
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
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 interfacejava.lang.CharSequence
- Overrides:
toString
in classjava.lang.Object
- See Also:
getValue()
-
toWString
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 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:
length
in 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:
subSequence
in interfacejava.lang.CharSequence
-