Class WString
- All Implemented Interfaces:
CharSequence
,Cloneable
,Comparable<WString>
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 TypeMethodDescriptionappend
(char[] buf) Appends a string, converting the string to a literal string if necessary.Appends a string, converting the string to a literal string if necessary.Appends a string, converting the string to a literal string if necessary.arg
(double value) Substitutes the next positional argument with a double value.arg
(int value) Substitutes the next positional argument with an integer value.arg
(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
Change the WString 's argument at position argIndex.char
charAt
(int index) Returns the character at a given index.clone()
int
Compares this WString object with the specified WString object for order.boolean
getArgs()
Returns the arguments.Utility method which returns the value as a JavaScript string literal.getJsStringLiteral
(char delimiter) Utility method which returns the value as a JavaScript string literal.getKey()
Returns the key for a localized string.int
Returns the string length.getValue()
Returns the value.getValue
(TextFormat format) Returns the value.boolean
isEmpty()
Returns whether the string is empty.boolean
Returns whether the string is literal.int
length()
Returns the string length.boolean
refresh()
Refreshes the (localized) strings.subSequence
(int start, int end) Returns a sub string.toString()
Returns the value of this string.static WString
toWString
(CharSequence charSequence) Converts a CharSequence to a WString.toXhtml()
Returns the XHTML-encoded value of this string.static WString
Creates a localized string.static WString
Creates a localized plural string.Methods inherited from class java.lang.Object
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
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
-
equals
-
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 interfaceCharSequence
- Returns:
- whether the string is empty.
-
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
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 interfaceComparable<WString>
-
tr
Creates a localized string.- Parameters:
key
- the key which is used to resolve within a locale.- Returns:
- the localized string.
- See Also:
-
trn
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
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:
-
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
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
Utility method which returns the value as a JavaScript string literal.Calls
getJsStringLiteral('\'')
- Returns:
- the string as a JavaScript string literal.
-
changeArg
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
Returns the value of this string.- Specified by:
toString
in interfaceCharSequence
- Overrides:
toString
in classObject
- See Also:
-
toXhtml
Returns the XHTML-encoded value of this string. -
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 interfaceCharSequence
-
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 interfaceCharSequence
-
getLength
public int getLength()Returns the string length.- Returns:
length()
-
subSequence
Returns a sub string.It is probably more efficient to call
getValue()
first and then compute on that String.- Specified by:
subSequence
in interfaceCharSequence
-