Wt: Wt::WString Class Reference

Wt 3.3.0

- Main Page
- Related Pages
- Modules
- Namespaces
- Classes

- Class List
- Class Hierarchy
- Class Members

Public Member Functions | Static Public Member Functions | Static Public Attributes | Related Functions
Wt::WString Class Reference

A value class which describes a locale-aware unicode string. More...

`#include <Wt/WString>`

List of all members.

## Public Member Functions
| WString ()
| Creates an empty string.
| WString (const wchar_t *value)
| Creates a WString from a wide C string.
| WString (const WString &other)
| Copy constructor.
| WString (const std::wstring &value)
| Creates a WString from a wide C++ string.
| WString (const char *value, CharEncoding encoding=LocalEncoding)
| Creates a WString from a C string.
| WString (const char *value, const std::locale &loc)
| Creates a WString from a C string.
| WString (const std::string &value, CharEncoding encoding=LocalEncoding)
| Creates a WString from a C++ string.
| WString (const std::string &value, const std::locale &loc)
| Creates a WString from a C++ string.
| ~WString ()
| Destructor.
WString & | operator= (const WString &rhs)
| Assignment operator.
bool | operator== (const WString &rhs) const
| Comparison operator.
bool | operator< (const WString &rhs) const
| Comparison operator.
bool | operator> (const WString &rhs) const
| Comparison operator.
WString & | operator+= (const WString &rhs)
| Self-concatenation operator.
WString & | operator+= (const std::wstring &rhs)
| Self-concatenation operator.
WString & | operator+= (const wchar_t *rhs)
| Self-concatenation operator.
WString & | operator+= (const std::string &rhs)
| Self-concatenation operator.
WString & | operator+= (const char *rhs)
| Self-concatenation operator.
bool | empty () const
| Returns whether the string is empty.
std::string | toUTF8 () const
| Returns the value as a UTF8 unicode encoded string.
std::wstring | value () const
| Returns the value as a wide C++ string.
std::string | narrow (const std::locale &loc=std::locale()) const
| Returns the value as a narrow C++ string.
| operator std::wstring () const
| Returns the value as a wide C++ string.
bool | literal () const
| Retuns whether the string is literal or localized.
const std::string | key () const
| Returns the key for a localized string.
WString & | arg (const std::wstring &value)
| Substitutes the next positional argument with a string value.
WString & | arg (const std::string &value, CharEncoding=LocalEncoding)
| Substitutes the next positional argument with a string value.
WString & | arg (const WString &value)
| Substitutes the next positional argument with a string value.
WString & | arg (int value)
| Substitutes the next positional argument with an integer value.
WString & | arg (unsigned value)
| Substitutes the next positional argument with an unsigned value.
WString & | arg (::int64_t value)
| Substitutes the next positional argument with an integer value.
WString & | arg (::uint64_t value)
| Substitutes the next positional argument with an unsigned value.
WString & | arg (double value)
| Substitutes the next positional argument with a double value.
const std::vector< WString > & | args () const
| Returns the list of arguments.
bool | refresh ()
| Refreshes the string.
std::string | jsStringLiteral (char delimiter= '\'') const
| Returns the string as a JavaScript literal.
bool | operator!= (const WString &rhs) const
| Comparison operator.
## Static Public Member Functions
static WString | fromUTF8 (const std::string &value, bool checkValid=false)
| Creates a WString from a UTF8 unicode encoded string.
static WString | fromUTF8 (const char *value, bool checkValid=false)
| Creates a WString from a UTF8 unicode encoded string.
static WString | tr (const char *key)
| Creates a localized string from a key.
static WString | tr (const std::string &key)
| Creates a localized string with the specified key.
static WString | trn (const char *key,::uint64_t n)
| Creates a localized string from a key for a number `n`.
static WString | trn (const std::string &key,::uint64_t n)
| Creates a localized string with the specified key for a number `n`.
## Static Public Attributes
static const WString | Empty
| An empty string.
## Related Functions
(Note that these are not member functions.)
enum | CharEncoding { LocalEncoding, UTF8 }
| Enumeration that indicates a character encoding. More...
typedef WString | WMessage
| Alias for WString (deprecated).
WT_API WString | operator+ (const WString &lhs, const WString &rhs)
| Concatenate two WStrings.
WT_API WString | operator+ (const WString &lhs, const std::wstring &rhs)
| Conatenate a WString with a C++ wide string.
WT_API WString | operator+ (const WString &lhs, const wchar_t *rhs)
| Conatenate a WString with a C wide string.
WT_API WString | operator+ (const WString &lhs, const std::string &rhs)
| Conatenate a WStrin with a C++ string.
WT_API WString | operator+ (const WString &lhs, const char *rhs)
| Conatenate a WString with a C string.
WT_API WString | operator+ (const std::wstring &lhs, const WString &rhs)
| Conatenate a C++ wide string with a WString.
WT_API WString | operator+ (const wchar_t *lhs, const WString &rhs)
| Conatenate a C wide string with a WString.
WT_API WString | operator+ (const std::string &lhs, const WString &rhs)
| Conatenate a C++ string with a WString.
WT_API WString | operator+ (const char *lhs, const WString &rhs)
| Conatenate a C string with a WString.
WT_API bool | operator== (const char *lhs, const WString &rhs)
| Compare a C string with a WString.
WT_API bool | operator== (const wchar_t *lhs, const WString &rhs)
| Compare a C wide string with a WString.
WT_API bool | operator== (const std::string &lhs, const WString &rhs)
| Compare a C++ string with a WString.
WT_API bool | operator== (const std::wstring &lhs, const WString &rhs)
| Compare a C++ wide string with a WString.
WT_API bool | operator!= (const char *lhs, const WString &rhs)
| Compare a C string with a WString.
WT_API bool | operator!= (const wchar_t *lhs, const WString &rhs)
| Compare a C wide string with a WString.
WT_API bool | operator!= (const std::string &lhs, const WString &rhs)
| Compare a C++ string with a WString.
WT_API bool | operator!= (const std::wstring &lhs, const WString &rhs)
| Compare a C++ wide string with a WString.
WT_API std::wostream & | operator<< (std::wostream &lhs, const WString &rhs)
| Output a WString to a C++ wide stream.
WT_API std::ostream & | operator<< (std::ostream &lhs, const WString &rhs)
| Output a WString to a C++ stream.
WT_API std::wstring | widen (const std::string &s, const std::locale &loc=std::locale())
| Convert a narrow to a wide string.
WT_API std::string | narrow (const std::wstring &s, const std::locale &loc=std::locale())
| Convert a wide to a narrow string.
WT_API std::wstring | fromUTF8 (const std::string &s)
| Decode a UTF8 string a wide string.
WT_API std::string | fromUTF8 (const std::string &s, const std::locale &loc)
| Decode a UTF8 string into a (narrow) string.
WT_API std::string | toUTF8 (const std::wstring &s)
| Encode a wide string to UTF8.
WT_API std::string | toUTF8 (const std::string &s, const std::locale &loc=std::locale())
| Encode an character string (encoding known) to UTF8.

## Detailed Description

A value class which describes a locale-aware unicode string.

Wt offers this string to facilitate handling of unicode text through the user interface, and to offer support for localized text using message resource bundles.

A WString may be constructed from a std::string, std::wstring or c-style strings (const char * and const wchar_t *), and converted to each of these strings taking into account the locale in which the Wt application runs on the web server. Independent of the locale on the web server, you may convert from and to UTF8 unicode encoded std::strings.

By using the static functions WString::tr() (or WWidget::tr()), one may construct a localized string. The key is used to retrieve its current value from the application's message-resource bundles.

Argument place holder in a string, denoted using {n} for the n'th argument, may be substituted by values set using arg(int) and arg(std::string).

WString is used by all built-in widgets for displayed text. By calling WApplication::setLocale() or WApplication::refresh(), the contents of every WString is reevaluated in the new locale, by calling refresh(). In this way, the contents of the whole user interface is adapted to a possibly changed locale.

To benefit from the localization properties of WString, you should design your own widget classes to use WString in their interface for any text that is displayed. In this way, your own widgets will automatically, and without any extra effort, participate in a relocalization triggered by WApplication::setLocale().

This string class does not provide anything more than basic manipulations. Instead, you should convert to a standard library string class to manipulate the string contents and perform string algorithms on them. In particular, we recommend to use the conversion methods toUTF8(), fromUTF8() to convert from and to UTF8 encoded std::strings. In this way, you can support the whole unicode character set, with backward compatible support for the standard 7-bit ASCII set. Since WString internally uses UTF8-encoding, and UTF8-encoding is used by the library for communication with the browser, there is no actual conversion overhead. Only when you need to run string algorithms that require the actual length of the string in number of characters, you would need to convert to a wide string representation such as std::wstring.

See also:
  WApplication::messageResourceBundle()
  WApplication::locale()

## Constructor & Destructor Documentation

Wt::WString::WString | ( |  | ) |

Creates an empty string.

Create a literal string with empty contents ("").

Wt::WString::WString | ( | const wchar_t * | value | ) |

Creates a WString from a wide C string.

The wide string is implicitly converted to proper unicode. Note that there are known issues with the portability of wchar_t since its width and encoding are platform dependent.

Wt::WString::WString | ( | const std::wstring & | value | ) |

Creates a WString from a wide C++ string.

The wide string is implicitly converted to proper unicode. Note that there are known issues with the portability of wchar_t since its width and encoding are platform dependent.

Wt::WString::WString | ( | const char * | value,
|  | CharEncoding | encoding = `LocalEncoding`
| ) |  |

Creates a WString from a C string.

The C string is implicitly converted to unicode. When `encoding` is LocalEncoding, the current locale is used to interpret the C string. When encoding is UTF8, the C string is interpreted as a UTF8 encoded unicode string.

Wt::WString::WString | ( | const char * | value,
|  | const std::locale & | loc
| ) |  |

Creates a WString from a C string.

The C string is implicitly converted to unicode. The string is interpreted within the character set of the given locale.

Wt::WString::WString | ( | const std::string & | value,
|  | CharEncoding | encoding = `LocalEncoding`
| ) |  |

Creates a WString from a C++ string.

The C++ string is implicitly converted to unicode. When `encoding` is LocalEncoding, the current locale is used to interpret the C++ string. When encoding is UTF8, the C++ string is interpreted as a UTF8 encoded unicode string.

Wt::WString::WString | ( | const std::string & | value,
|  | const std::locale & | loc
| ) |  |

Creates a WString from a C++ string.

The C++ string is implicitly converted to unicode. The string is interpreted within the character set of the given locale.

## Member Function Documentation

WString & Wt::WString::arg | ( | const std::wstring & | 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 "<tt>{1} bought {2} apples in the shop.</tt>" with first argument value "<tt>Bart</tt>" and second argument value `5` becomes: "<tt>Bart bought 5 apples in the shop.</tt>"

WString & Wt::WString::arg | ( | const std::string & | value,
|  | CharEncoding | encoding = `LocalEncoding`
| ) |  |

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 "<tt>{1} bought {2} apples in the shop.</tt>" with first argument value "<tt>Bart</tt>" and second argument value `5` becomes: "<tt>Bart bought 5 apples in the shop.</tt>"

WString & Wt::WString::arg | ( | const WString & | 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 "<tt>{1} bought {2} apples in the shop.</tt>" with first argument value "<tt>Bart</tt>" and second argument value `5` becomes: "<tt>Bart bought 5 apples in the shop.</tt>"

WString & Wt::WString::arg | ( | int | value | ) |

Substitutes the next positional argument with an integer value.

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

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

WString & Wt::WString::arg | ( | unsigned | value | ) |

Substitutes the next positional argument with an unsigned value.

See also:
  arg()

WString & Wt::WString::arg | ( | ::int64_t | value | ) |

Substitutes the next positional argument with an integer value.

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

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

WString & Wt::WString::arg | ( | ::uint64_t | value | ) |

Substitutes the next positional argument with an unsigned value.

See also:
  arg()

WString & Wt::WString::arg | ( | double | value | ) |

Substitutes the next positional argument with a double value.

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

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

WString Wt::WString::fromUTF8 | ( | const std::string & | value,
|  | bool | checkValid = `false`
| ) |  | `[static]`

Creates a WString from a UTF8 unicode encoded string.

This is equivalent to using the constructor WString(`value`, UTF8).

When `checkValid` is `true`, the UTF8 encoding is validated. You should enable this only if you cannot trust the origin of the string. The library uses this internally whenever it receives data from the browser (in UTF-8 format).

WString Wt::WString::fromUTF8 | ( | const char * | value,
|  | bool | checkValid = `false`
| ) |  | `[static]`

Creates a WString from a UTF8 unicode encoded string.

This is equivalent to using the constructor WString(`value`, UTF8).

When `checkValid` is `true`, the UTF8 encoding is validated. You should enable this only if you cannot trust the origin of the string. The library uses this internally whenever it receives data from the browser (in UTF-8 format).

std::string Wt::WString::jsStringLiteral | ( | char | delimiter = `'\''` | ) | const

Returns the string as a JavaScript literal.

The `delimiter` may be a single or double quote.

See also:
  WWebWidget::jsStringLiteral()

const std::string Wt::WString::key | ( |  | ) | const

Returns the key for a localized string.

When the string is literal, the result is undefined.

bool Wt::WString::literal | ( |  | ) | const

Retuns whether the string is literal or localized.

See also:
  tr()

std::string Wt::WString::narrow | ( | const std::locale & | loc = `std::locale()` | ) | const

Returns the value as a narrow C++ string.

A localized string is resolved using the WApplication::localizedStrings().

Argument place holders are substitued with actual arguments.

Any wide character is narrowed using the provided locale, possibly losing information. If you wish to keep all information, use toUTF8() instead, which encodes wide characters in the string.

See also:
  toUTF8()

Wt::WString::operator std::wstring | ( |  | ) | const

Returns the value as a wide C++ string.

A localized string is resolved using the WApplication::localizedStrings().

Argument place holders are substitued with actual arguments.

bool Wt::WString::operator!= | ( | const WString & | rhs | ) | const

Comparison operator.

Compares two strings and returns `true` if the strings are not exactly the same. This may require evaluating a localized string in the current locale.

WString & Wt::WString::operator+= | ( | const WString & | rhs | ) |

Self-concatenation operator.

Appends a string to the current value. If the string was localized, this automatically converts it to a literal string, by evaluating the string using the current locale.

WString & Wt::WString::operator+= | ( | const std::wstring & | rhs | ) |

Self-concatenation operator.

Appends a string to the current value. If the string was localized, this automatically converts it to a literal string, by evaluating the string using the current locale.

WString & Wt::WString::operator+= | ( | const wchar_t * | rhs | ) |

Self-concatenation operator.

Appends a string to the current value. If the string was localized, this automatically converts it to a literal string, by evaluating the string using the current locale.

WString & Wt::WString::operator+= | ( | const std::string & | rhs | ) |

Self-concatenation operator.

Appends a string to the current value. The right hand side is interpreted in the server locale and converted to unicode. If the string was localized, this automatically converts it to a literal string, by evaluating the string using the current locale.

WString & Wt::WString::operator+= | ( | const char * | rhs | ) |

Self-concatenation operator.

Appends a string to the current value. The right hand side is interpreted in the server locale and converted to unicode. If the string was localized, this automatically converts it to a literal string, by evaluating the string using the current locale.

bool Wt::WString::operator< | ( | const WString & | rhs | ) | const

Comparison operator.

Compares to strings lexicographically. This may require evaluating a localized string in the current locale. The unicode representation of the strings are compared.

WString & Wt::WString::operator= | ( | const WString & | rhs | ) |

Assignment operator.

Copy another string into this string.

bool Wt::WString::operator== | ( | const WString & | rhs | ) | const

Comparison operator.

Compares two strings and returns `true` if the strings are exactly the same. This may require evaluating a localized string in the current locale.

bool Wt::WString::operator> | ( | const WString & | rhs | ) | const

Comparison operator.

Compares to strings lexicographically. This may require evaluating a localized string in the current locale. The unicode representation of the strings are compared.

bool Wt::WString::refresh | ( |  | ) |

Refreshes the string.

For a localized string, its value is resolved again.

Returns whether the value has (potentially) changed.

std::string Wt::WString::toUTF8 | ( |  | ) | const

Returns the value as a UTF8 unicode encoded string.

For a localized string, returns the current localized value.

See also:
  fromUTF8()

WString Wt::WString::tr | ( | const char * | key | ) | `[static]`

Creates a localized string from a key.

Whenever the value of the string is needed, the key is used for a lookup in the application message resource bundles taking into account the current application locale. If the key cannot be resolved, its value is set to '??key??'.

See also:
  WApplication::locale(), WApplication::localizedStrings()

WString Wt::WString::tr | ( | const std::string & | key | ) | `[static]`

Creates a localized string with the specified key.

See also:
  tr(const char *)

WString Wt::WString::trn | ( | const char * | key,
|  | ::uint64_t | n
| ) |  | `[static]`

Creates a localized string from a key for a number `n`.

Whenever the value of the string is needed, the `key` is used for a lookup in the application message resource bundles taking into account the current application locale. This function fetches the appropriate plural case for the translation corresponding to the quantity `n`. Note that usually, your string will have a place-holder for the value of `n`, and thus you will also need to bind `as` an argument.

For example, consider a string "quantity.cars" with two plural cases:

- n == 1: "{1} car"
- n != 1: "{1} cars"

You would use the following to use the string:

     Wt::WString::trn("quantity.cars", cars).arg(cars);

If the `key` cannot be resolved, its value is set to '??key??'.

See also:
  tr()

WString Wt::WString::trn | ( | const std::string & | key,
|  | ::uint64_t | n
| ) |  | `[static]`

Creates a localized string with the specified key for a number `n`.

See also:
  trn(const char *)

std::wstring Wt::WString::value | ( |  | ) | const

Returns the value as a wide C++ string.

A localized string is resolved using the WApplication::localizedStrings().

Argument place holders are substitued with actual arguments.

## Friends And Related Function Documentation

enum CharEncoding`[related]`

Enumeration that indicates a character encoding.

Character encodings are used to represent characters in a stream of bytes.

Enumerator:

  LocalEncoding | The (server) system locale. Each byte represents a character, according to the locale of the server-side system.
  UTF8 | UTF-8 unicode encoding. The byte stream represents unicode characters encoded using UTF-8.

WT_API std::wstring fromUTF8 | ( | const std::string & | s | ) | `[related]`

Decode a UTF8 string a wide string.

Decode a UTF8 string to a wide string. In a UTF8 encoded unicode string, some unicode characters are represented in more than one byte. This method will decode to extract the proper unicode characters from the string. The resulting string may thus be shorter (has less characters) than the original, but does not lead to a loss of information.

See also:
  toUTF8(const std::string& s), narrow(const std::wstring&)

WT_API std::string fromUTF8 | ( | const std::string & | s,
|  | const std::locale & | loc
| ) |  | `[related]`

Decode a UTF8 string into a (narrow) string.

Decode a UTF8 string to a normal string. Not all Unicode characters can be represented in a narrow string, and quite a lot characters will have no equivalent in the target character set, so you may loose information.

To distinguish from the other fromUTF8() function, that returns a wstring, the locale is not an optional argument, as in most other locale-conversing functions. You may choose to use the default-constructed std::locale().

See also:
  toUTF8(const std::string& s, const std::locale &), fromUTF8(const std::string& s)

WT_API std::string narrow | ( | const std::wstring & | s,
|  | const std::locale & | loc = `std::locale()`
| ) |  | `[related]`

Convert a wide to a narrow string.

Convert a wide string to a narrow string. This method will encode the characters in the given locale, if possible.

In general this will lead to a loss of information. If you wish to preserve all information, you should us toUTF8() instead.

See also:
  widen(const std::string&), toUTF8(const std::wstring& s)

WT_API std::ostream & operator<< | ( | std::ostream & | lhs,
|  | const WString & | rhs
| ) |  | `[related]`

Output a WString to a C++ stream.

The string is narrowed using the currently global C++ locale, possibly losing information.

WT_API std::string toUTF8 | ( | const std::wstring & | s | ) | `[related]`

Encode a wide string to UTF8.

Convert a wide string to UTF8. This method will encode the given wide string in UTF8. This may result in a string that is possibly longer (has more characters), but does not lead to a loss of information.

See also:
  fromUTF8(const std::string& s), narrow(const std::wstring&)

WT_API std::string toUTF8 | ( | const std::string & | s,
|  | const std::locale & | loc = `std::locale()`
| ) |  | `[related]`

Encode an character string (encoding known) to UTF8.

Convert a char * string to UTF8. This method will encode the given string in UTF8, assuming that the original string was encoded in the given locale. This conversion does not lead to a loss of information.

The reverse operation is in principle narrow(fromUTF8(str), locale).

Do not call this function multiple times: toUTF8(toUTF8(str)) is meaningless.

See also:
  toUTF8(const std::wstring& s), fromUTF8(const std::string &), narrow(const std::wstring&)

WT_API std::wstring widen | ( | const std::string & | s,
|  | const std::locale & | loc = `std::locale()`
| ) |  | `[related]`

Convert a narrow to a wide string.

Convert a narrow string to a wide string. This method will interpret the input string as being encoded in the given locale (by default the currently configured global C++ locale).

See also:
  narrow(const std::wstring&), fromUTF8(const std::string& s)

typedef WString WMessage`[related]`

Alias for WString (deprecated).

Deprecated:
  use WString instead.

WString provides all the functionality of the now deprecated WMessage, but is used more consistently through the API.

All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator