| Version: 3.3.1

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)
| Macro taking a literal string and expanding into a wide string. More...
#define | wxT_2(string)
| Obsolete macro which simply expands to its argument. 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 similar to _() but for the plural variant of wxGetTranslation(). More...
#define | wxGETTEXT_IN_CONTEXT(context, string)
| Similar to _() but translates the string in the given context. More...
#define | wxGETTEXT_IN_CONTEXT_PLURAL(context, string, plural, n)
| Similar to wxPLURAL() but translates the string in the given context. More...
#define | wxTRANSLATE(string)
| This macro doesn't do anything in the program code – it simply expands to the value of its argument. More...
#define | wxTRANSLATE_IN_CONTEXT(context, string)
| This macro doesn't do anything in the program code – it simply expands to the value of its argument. More...
#define | _(string)
| Macro to be used around all literal strings that should be translated. More...

## Typedefs
typedef wchar_t | wxChar
| wxChar is a compatibility typedef always defined as `wchar_t` now. More...
typedef wchar_t | wxSChar
| wxSChar is a compatibility typedef always defined as `wchar_t` now. More...
typedef wchar_t | wxUChar
| wxUChar is a compatibility typedef always defined as `wchar_t` now. More...
typedef wxUSE_UNICODE_WCHAR_dependent | wxStringCharType
| wxStringCharType is defined to be: - `char` when `wxUSE_UNICODE_WCHAR==0` - `wchar_t` when `wxUSE_UNICODE_WCHAR==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...
wxString | wxASCII_STR (const char *s)
| Convenience macro for explicitly constructing wxString from ASCII strings. 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, const wxString &context=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, const wxString &context=wxEmptyString)
| This is an overloaded version of wxGetTranslation(const wxString&, const wxString&, const wxString&), please see its documentation for general information. More...

## Macro Definition Documentation

## ◆ _

#define _ | ( |  | 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>

## ◆ _T

#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>

## ◆ wxGETTEXT_IN_CONTEXT

#define wxGETTEXT_IN_CONTEXT | ( |  | context,
|  |  | string
| ) |  |

Similar to _() but translates the string in the given context.

See the description of `context` argument of wxGetTranslation().

The arguments must be string literals.

See also
  wxGETTEXT_IN_CONTEXT_PLURAL()

Since
  3.1.1

## ◆ wxGETTEXT_IN_CONTEXT_PLURAL

#define wxGETTEXT_IN_CONTEXT_PLURAL | ( |  | context,
|  |  | string,
|  |  | plural,
|  |  | n
| ) |  |

Similar to wxPLURAL() but translates the string in the given context.

The string arguments must be string literals.

See the description of `context` argument of wxGetTranslation().

See also
  wxGETTEXT_IN_CONTEXT()

Since
  3.1.1

## ◆ wxPLURAL

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

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

The string arguments must be string literals.

Returns
  A const wxString.

Include file:

    #include <wx/intl.h>

## ◆ wxS

#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>

## ◆ wxT

#define wxT | ( |  | string | ) |

Macro taking a literal string and expanding into a wide string.

This macro should not be used in the new code any more as it is simply equivalent to using `L` string prefix now, i.e. its simplified definition could be just

#define wxT(x) L##x

It used to be required when converting literal strings to wxString in wxWidgets versions prior to 2.9.0, and so can be found in a lot of existing code, but can be simply removed in any code using more recent versions of wxWidgets.

See also
  Unicode Support in wxWidgets, wxS()

Include file:

    #include <wx/chartype.h>

## ◆ wxT_2

#define wxT_2 | ( |  | string | ) |

Obsolete macro which simply expands to its argument.

This macro could be used in the code which needed to compile with both wxWidgets 2 and 3 versions in some rare circumstances. It is still provided for compatibility but serves no purpose any longer.

See also
  wxT()

Since
  2.8.12, 2.9.2

Include file:

    #include <wx/chartype.h>

## ◆ wxTRANSLATE

#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 names of chemical elements, which have to be translated. If you write:

static const char * const elements[] = { _("Hydrogen"), _("Helium"), ... };
...
// use elements[n] as usual
_
#define _(string)
Macro to be used around all literal strings that should be translated.
Definition: translation.h:658

The code would compile and run, but there would be no translations for the strings because static variables are initialized at a very early stage of program execution; that is, before the locale and paths to message catalog files have been set up. So instead you should do this:

static const char * const elements[] = { wxTRANSLATE("Hydrogen"),
wxTRANSLATE("Helium"), ... };
...
// use wxGetTranslation(elements[n])
wxTRANSLATE
#define wxTRANSLATE(string)
This macro doesn't do anything in the program code – it simply expands to the value of its argument.
Definition: translation.h:563

Note that if you simply omit wxTRANSLATE() above, those strings would not be marked for translation, and would therefore not be included in the message catalog. Consequently, wxGetTranslation() would not find translations for them.

Returns
  A const wxChar*.

Include file:

    #include <wx/intl.h>

## ◆ wxTRANSLATE_IN_CONTEXT

#define wxTRANSLATE_IN_CONTEXT | ( |  | context,
|  |  | string
| ) |  |

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

Similar to wxTRANSLATE(), but with a context argument. Both the context and string need to be string literals. Be sure to use the same context when calling wxGetTranslation().

Since
  3.1.7

## Typedef Documentation

## ◆ wxChar

typedef wchar_t wxChar

wxChar is a compatibility typedef always defined as `wchar_t` now.

Note that it is not affected by `wxUSE_UNICODE_UTF8` option.

## ◆ wxSChar

typedef wchar_t wxSChar

wxSChar is a compatibility typedef always defined as `wchar_t` now.

## ◆ wxStringCharType

typedef wxUSE_UNICODE_WCHAR_dependent wxStringCharType

wxStringCharType is defined to be: - `char` when `wxUSE_UNICODE_WCHAR==0` - `wchar_t` when `wxUSE_UNICODE_WCHAR==1`

The `wxUSE_UNICODE_WCHAR` symbol is defined by default, but may be turned off in which case `wxUSE_UNICODE_UTF8` is turned on.

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

## ◆ wxUChar

typedef wchar_t wxUChar

wxUChar is a compatibility typedef always defined as `wchar_t` now.

## Function Documentation

## ◆ wxASCII_STR()

wxString wxASCII_STR | ( | const char * | s | ) |

Convenience macro for explicitly constructing wxString from ASCII strings.

This macro simply expands to a call to wxString::FromAscii() but is slightly shorter.

Since
  3.1.4

## ◆ wxGetTranslation() [1/2]

const wxString& wxGetTranslation | ( | const wxString & | string,
|  | const wxString & | domain = `wxEmptyString`,
|  | const wxString & | context = `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. If you enable logging of trace messages with "i18n" mask (using wxLog::AddTraceMask()) and debug logging is enabled (see Debugging), a message is also logged in this case – which helps 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 nearly the same thing as wxGetTranslation(), with the exception that the argument to _() must be a string literal.

If context is not empty (notice that this argument is only available starting from wxWidgets 3.1.1), item translation is looked up in the specified context. This allows having different translations for the same string appearing in different contexts, e.g. it may be necessary to translate the same English "Open" verb differently depending on the object it applies to. To do this, you need to use `msgctxt` in the source message catalog and specify different contexts for the different occurrences of the string and then use the same contexts in the calls to this function (or wxGETTEXT_IN_CONTEXT() or wxGETTEXT_IN_CONTEXT_PLURAL() macros).

This function is thread-safe.

Note
  If you want literal strings inside of this function to be translated, you need to use `-kwxGetTranslation` with `xgettext`. Also, don't use wxT() inside of this function with string literals, as `xgettext` won't be able to extract them.

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

Include file:

    #include <wx/intl.h>

## ◆ wxGetTranslation() [2/2]

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

This is an overloaded version of wxGetTranslation(const wxString&, 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&, 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>

## ◆ wxJoin()

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(). The escape characters themselves are not escaped when they occur in the middle of the arr elements, but are escaped when they occur at the end, i.e.

wxArrayString arr;
arr.push_back("foo^");
arr.push_back("bar^baz");
wxPuts(wxJoin(arr, ':', '^')); // prints "foo^^:bar^baz"
wxArrayString
wxArrayString is a legacy class similar to std::vector<wxString>.
Definition: arrstr.h:67
wxJoin
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 resul...

In any case, applying wxSplit() to the result of wxJoin() is guaranteed to recover the original array.

See also
  wxSplit()

Include file:

    #include <wx/arrstr.h>

## ◆ wxSplit()

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>

## ◆ wxStringCheck()

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

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

Allows extending a function with the signature:

bool SomeFunc(const wxUniChar& c)
wxUniChar
This class represents a single Unicode character.
Definition: unichar.h:18

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:

if (wxStringCheck<wxIsdigit>(myString))
... // the entire string contains only digits!
else
... // 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.

## ◆ wxStringTokenize()

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>