wxWidgets: Strings

| Version: 3.0.5

- Main Page
- Related Pages
- Categories
- Classes
- Files

All Classes Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
Macros | Typedefs | Functions
Strings
Functions and Macros by Category

## Detailed Description

Global string functions and macros.

See wxString for the wxWidgets string class.

Please note that all functions of this group which are documented to take `char*` arrays are overloaded with `wchar_t*` variants.

Note also that wxWidgets wraps all standard CRT functions, even if the wrappers are not (all) documented.

## Macros
#define | wxT(string)
| This macro can be used with character and string literals (in other words, `'x'` or `"foo"`) to automatically convert them to wide strings in Unicode builds of wxWidgets. More...
#define | wxT_2(string)
| Compatibility macro which expands to wxT() in wxWidgets 2 only. More...
#define | wxS(string)
| wxS is a macro which can be used with character and string literals (in other words, `'x'` or `"foo"`) to convert them either to wide characters or wide strings in `wchar_t-based` (UTF-16) builds, or to keep them unchanged in `char-based` (UTF-8) builds. More...
#define | _T(string)
| This macro is exactly the same as wxT() and is defined in wxWidgets simply because it may be more intuitive for Windows programmers as the standard Win32 headers also define it (as well as yet another name for the same macro which is _TEXT()). More...
#define | wxPLURAL(string, plural, n)
| This macro is identical to _() but for the plural variant of wxGetTranslation(). More...
#define | wxTRANSLATE(string)
| This macro doesn't do anything in the program code – it simply expands to the value of its argument. More...

## Typedefs
typedef wxUSE_UNICODE_dependent | wxChar
| wxChar is defined to be `char` when `wxUSE_UNICODE==0` `wchar_t` when `wxUSE_UNICODE==1` (the default). More...
typedef wxUSE_UNICODE_dependent | wxSChar
| wxSChar is defined to be `signed char` when `wxUSE_UNICODE==0` `wchar_t` when `wxUSE_UNICODE==1` (the default). More...
typedef wxUSE_UNICODE_dependent | wxUChar
| wxUChar is defined to be `unsigned char` when `wxUSE_UNICODE==0` `wchar_t` when `wxUSE_UNICODE==1` (the default). More...
typedef wxUSE_UNICODE_WCHAR_dependent | wxStringCharType
| wxStringCharType is defined to be: `char` when `wxUSE_UNICODE==0` `char` when `wxUSE_UNICODE_WCHAR==0` and `wxUSE_UNICODE==1` `wchar_t` when `wxUSE_UNICODE_WCHAR==1` and `wxUSE_UNICODE==1` More...

## Functions
wxArrayString | wxSplit (const wxString &str, const wxChar sep, const wxChar escape= '\\')
| Splits the given wxString object using the separator sep and returns the result as a wxArrayString. More...
wxString | wxJoin (const wxArrayString &arr, const wxChar sep, const wxChar escape= '\\')
| Concatenate all lines of the given wxArrayString object using the separator sep and returns the result as a wxString. More...
template<bool(T)(const wxUniChar &c) >
bool | wxStringCheck (const wxString &val)
| Allows extending a function with the signature: More...
wxArrayString | wxStringTokenize (const wxString &str, const wxString &delims=wxDEFAULT_DELIMITERS, wxStringTokenizerMode mode=wxTOKEN_DEFAULT)
| This is a convenience function wrapping wxStringTokenizer which simply returns all tokens found in the given str as an array. More...
const wxString & | wxGetTranslation (const wxString &string, const wxString &domain=wxEmptyString)
| This function returns the translation of string in the current `locale()`. More...
const wxString & | wxGetTranslation (const wxString &string, const wxString &plural, unsigned n, const wxString &domain=wxEmptyString)
| This is an overloaded version of wxGetTranslation(const wxString&, const wxString&), please see its documentation for general information. More...
const wxString & | _ (const wxString &string)
| Macro to be used around all literal strings that should be translated. More...

## Macro Definition Documentation

#define _T | ( |  | string | ) |

This macro is exactly the same as wxT() and is defined in wxWidgets simply because it may be more intuitive for Windows programmers as the standard Win32 headers also define it (as well as yet another name for the same macro which is _TEXT()).

Don't confuse this macro with _()!

Note that since wxWidgets 2.9.0 the use of _T() is discouraged just like for wxT() and also that this macro may conflict with identifiers defined in standard headers of some compilers (such as Sun CC) so its use should really be avoided.

Include file:

    #include <wx/chartype.h>

#define wxPLURAL | ( |  | string,
|  |  | plural,
|  |  | n
| ) |  |

This macro is identical to _() but for the plural variant of wxGetTranslation().

Returns
  A const wxString.

Include file:

    #include <wx/intl.h>

#define wxS | ( |  | string | ) |

wxS is a macro which can be used with character and string literals (in other words, `'x'` or `"foo"`) to convert them either to wide characters or wide strings in `wchar_t-based` (UTF-16) builds, or to keep them unchanged in `char-based` (UTF-8) builds.

Basically this macro produces characters or strings of type wxStringCharType.

The use of this macro is optional as the translation will always be done at run-time even if there is a mismatch between the kind of the literal used and the string or character type used in the current build. However using it can be beneficial in performance-sensitive code to do the conversion at compile-time instead.

See also
  Unicode Support in wxWidgets, wxT()

Include file:

    #include <wx/chartype.h>

#define wxT | ( |  | string | ) |

This macro can be used with character and string literals (in other words, `'x'` or `"foo"`) to automatically convert them to wide strings in Unicode builds of wxWidgets.

This macro simply returns the value passed to it without changes in ASCII build. In fact, its definition is:

1 #ifdef UNICODE
2 # define wxT(x) L##x
3 #else // !Unicode
4 # define wxT(x) x
5 #endif

Note that since wxWidgets 2.9.0 you shouldn't use wxT() anymore in your program sources (it was previously required if you wanted to support Unicode).

See also
  Unicode Support in wxWidgets, wxS()

Include file:

    #include <wx/chartype.h>

#define wxT_2 | ( |  | string | ) |

Compatibility macro which expands to wxT() in wxWidgets 2 only.

This macro can be used in code which needs to compile with both wxWidgets 2 and 3 versions, in places where the wx2 API requires a Unicode string (in Unicode build) but the wx3 API only accepts a standard narrow string, as in e.g. wxCmdLineEntryDesc structure objects initializers.

Example of use:

1 const wxCmdLineEntryDesc cmdLineDesc[] =
2 {
3 { wxCMD_LINE_SWITCH, wxT_2("q"), wxT_2("quiet"),
4 wxT_2("Don't output verbose messages") },
5 wxCMD_LINE_DESC_END
6 };

Without `wxT_2` the code above wouldn't compile with wxWidgets 2, but using `wxT` instead, it wouldn't compile with wxWidgets 3.

See also
  wxT()

Since
  2.8.12, 2.9.2

Include file:

    #include <wx/chartype.h>

#define wxTRANSLATE | ( |  | string | ) |

This macro doesn't do anything in the program code – it simply expands to the value of its argument.

However it does have a purpose which is to mark the literal strings for the extraction into the message catalog created by `xgettext` program. Usually this is achieved using _() but that macro not only marks the string for extraction but also expands into a wxGetTranslation() call which means that it cannot be used in some situations, notably for static array initialization.

Here is an example which should make it more clear: suppose that you have a static array of strings containing the weekday names and which have to be translated (note that it is a bad example, really, as wxDateTime already can be used to get the localized week day names already). If you write:

1 static const char * const weekdays[] = { _("Mon"), ..., _("Sun") };
2 ...
3 // use weekdays[n] as usual

The code wouldn't compile because the function calls are forbidden in the array initializer. So instead you should do this:

1 static const char * const weekdays[] = { wxTRANSLATE("Mon"), ...,
2 wxTRANSLATE("Sun") };
3 ...
4 // use wxGetTranslation(weekdays[n])

Note that although the code would compile if you simply omit wxTRANSLATE() in the above, it wouldn't work as expected because there would be no translations for the weekday names in the program message catalog and wxGetTranslation() wouldn't find them.

Returns
  A const wxChar*.

Include file:

    #include <wx/intl.h>

## Typedef Documentation

typedef wxUSE_UNICODE_dependent wxChar

wxChar is defined to be

- `char` when `wxUSE_UNICODE==0`
- `wchar_t` when `wxUSE_UNICODE==1` (the default).

typedef wxUSE_UNICODE_dependent wxSChar

wxSChar is defined to be

- `signed char` when `wxUSE_UNICODE==0`
- `wchar_t` when `wxUSE_UNICODE==1` (the default).

typedef wxUSE_UNICODE_WCHAR_dependent wxStringCharType

wxStringCharType is defined to be:

- `char` when `wxUSE_UNICODE==0`
- `char` when `wxUSE_UNICODE_WCHAR==0` and `wxUSE_UNICODE==1`
- `wchar_t` when `wxUSE_UNICODE_WCHAR==1` and `wxUSE_UNICODE==1`

The `wxUSE_UNICODE_WCHAR` symbol is defined to `1` when building on Windows while it's defined to `0` when building on Unix, Linux or OS X. (Note that `wxUSE_UNICODE_UTF8` symbol is defined as the opposite of `wxUSE_UNICODE_WCHAR`.)

Note that wxStringCharType (as the name says) is the type used by wxString for internal storage of the characters.

typedef wxUSE_UNICODE_dependent wxUChar

wxUChar is defined to be

- `unsigned char` when `wxUSE_UNICODE==0`
- `wchar_t` when `wxUSE_UNICODE==1` (the default).

## Function Documentation

const wxString& _ | ( | const wxString & | string | ) |

Macro to be used around all literal strings that should be translated.

This macro expands into a call to wxGetTranslation(), so it marks the message for the extraction by `xgettext` just as wxTRANSLATE() does, but also returns the translation of the string for the current locale during execution.

This macro is thread-safe.

Include file:

    #include <wx/intl.h>

const wxString& wxGetTranslation | ( | const wxString & | string,
|  | const wxString & | domain = `wxEmptyString`
| ) |  |

This function returns the translation of string in the current `locale()`.

If the string is not found in any of the loaded message catalogs (see Internationalization), the original string is returned. In debug build, an error message is logged – this should help to find the strings which were not yet translated. If domain is specified then only that domain/catalog is searched for a matching string. As this function is used very often, an alternative (and also common in Unix world) syntax is provided: the _() macro is defined to do the same thing as wxGetTranslation().

This function is thread-safe.

Note
  This function is not suitable for literal strings in Unicode builds since the literal strings must be enclosed in wxT() macro which makes them unrecognised by `xgettext`, and so they are not extracted to the message catalog. Instead, use the _() and wxPLURAL() macro for all literal strings.

See also
  wxGetTranslation(const wxString&, const wxString&, unsigned, const wxString&)

Include file:

    #include <wx/intl.h>

const wxString& wxGetTranslation | ( | const wxString & | string,
|  | const wxString & | plural,
|  | unsigned | n,
|  | const wxString & | domain = `wxEmptyString`
| ) |  |

This is an overloaded version of wxGetTranslation(const wxString&, const wxString&), please see its documentation for general information.

This version is used when retrieving translation of string that has different singular and plural forms in English or different plural forms in some other language. Like wxGetTranslation(const wxString&,const wxString&), the string parameter must contain the singular form of the string to be converted and is used as the key for the search in the catalog. The plural parameter is the plural form (in English). The parameter n is used to determine the plural form. If no message catalog is found, string is returned if "n == 1", otherwise plural is returned.

See GNU gettext Manual for additional information on plural forms handling: http://www.gnu.org/software/gettext/manual/gettext.html#Plural-forms For a shorter alternative see the wxPLURAL() macro.

This function is thread-safe.

Include file:

    #include <wx/intl.h>

wxString wxJoin | ( | const wxArrayString & | arr,
|  | const wxChar | sep,
|  | const wxChar | escape = `'\\'`
| ) |  |

Concatenate all lines of the given wxArrayString object using the separator sep and returns the result as a wxString.

If the escape character is non-NULL, then it's used as prefix for each occurrence of sep in the strings contained in arr before joining them which is necessary in order to be able to recover the original array contents from the string later using wxSplit().

See also
  wxSplit()

Include file:

    #include <wx/arrstr.h>

wxArrayString wxSplit | ( | const wxString & | str,
|  | const wxChar | sep,
|  | const wxChar | escape = `'\\'`
| ) |  |

Splits the given wxString object using the separator sep and returns the result as a wxArrayString.

If the escape character is non-NULL, then the occurrences of sep immediately prefixed with escape are not considered as separators. Note that empty tokens will be generated if there are two or more adjacent separators.

See also
  wxJoin()

Include file:

    #include <wx/arrstr.h>

template<bool(T)(const wxUniChar &c) >

bool wxStringCheck | ( | const wxString & | val | ) |
| inline

Allows extending a function with the signature:

1 bool SomeFunc(const wxUniChar& c)

which operates on a single character, to an entire wxString.

E.g. if you want to check if an entire string contains only digits, you can do:

1 if (wxStringCheck<wxIsdigit>(myString))
2 ... // the entire string contains only digits!
3 else
4 ... // at least one character of myString is not a digit

Returns
  true if the given function returns a non-zero value for all characters of the val string.

wxArrayString wxStringTokenize | ( | const wxString & | str,
|  | const wxString & | delims = `wxDEFAULT_DELIMITERS`,
|  | wxStringTokenizerMode | mode = `wxTOKEN_DEFAULT`
| ) |  |

This is a convenience function wrapping wxStringTokenizer which simply returns all tokens found in the given str as an array.

Please see wxStringTokenizer::wxStringTokenizer for the description of the other parameters.

Returns
  The array with the parsed tokens.

Include file:

    #include <wx/tokenzr.h>