Strings | wxWidgets

| Version: 3.2.11

Loading...
Searching...
No Matches
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.
#define | wxT_2(string)
| Compatibility macro which expands to wxT() in wxWidgets 2 only.
#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.
#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()).
#define | wxPLURAL(string, plural, n)
| This macro is identical to _() but for the plural variant of wxGetTranslation().
#define | wxGETTEXT_IN_CONTEXT(context, string)
| Similar to _() but translates the string in the given context.
#define | wxGETTEXT_IN_CONTEXT_PLURAL(context, string, plural, n)
| Similar to wxPLURAL() but translates the string in the given context.
#define | wxTRANSLATE(string)
| This macro doesn't do anything in the program code – it simply expands to the value of its argument.
#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.

## Typedefs
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_dependent | wxUChar
| wxUChar is defined to be - unsigned 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

## 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.
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.
template<bool(T)(const wxUniChar &c)>
bool | wxStringCheck (const wxString &val)
| Allows extending a function with the signature:
wxString | wxASCII_STR (const char *s)
| Convenience macro for explicitly constructing wxString from ASCII strings.
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.
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()`.
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&), please see its documentation for general information.
const wxString & | _ (const wxString &string)
| Macro to be used around all literal strings that should be translated.

## Macro Definition Documentation

## ◆ _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().

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.

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 identical to _() but for the plural variant of wxGetTranslation().

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

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:

#ifdef UNICODE
# define wxT(x) L##x
#else // !Unicode
# define wxT(x) x
#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>

## ◆ wxT_2

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

const wxCmdLineEntryDesc cmdLineDesc[] =
{
{ wxCMD_LINE_SWITCH, wxT_2("q"), wxT_2("quiet"),
wxT_2("Don't output verbose messages") },
wxCMD_LINE_DESC_END
};
wxCMD_LINE_SWITCH
@ wxCMD_LINE_SWITCH
A boolean argument of the program; e.g. -v to enable verbose mode.
Definition cmdline.h:69
wxT_2
#define wxT_2(string)
Compatibility macro which expands to wxT() in wxWidgets 2 only.
Definition chartype.h:61
wxCmdLineEntryDesc
The structure wxCmdLineEntryDesc is used to describe a command line switch, option or parameter.
Definition cmdline.h:120

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>

## ◆ 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
_
const wxString & _(const wxString &string)
Macro to be used around all literal strings that should be translated.

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:586

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 wxUSE_UNICODE_dependent wxChar

wxChar is defined to be - `char` when wxUSE_UNICODE==0 - `wchar_t` when wxUSE_UNICODE==1 (the default).

## ◆ wxSChar

typedef wxUSE_UNICODE_dependent wxSChar

wxSChar is defined to be - signed char when wxUSE_UNICODE==0 - `wchar_t` when wxUSE_UNICODE==1 (the default).

## ◆ wxStringCharType

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 macOS. (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.

## ◆ wxUChar

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>

## ◆ 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 the same thing as wxGetTranslation().

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
  This function is not suitable for literal strings using wxT() macro since this macro is not recognised by `xgettext`, and so such strings 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>

## ◆ 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&), 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>

## ◆ 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 an efficient container for storing wxString objects.
Definition arrstr.h:54
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
wxStringCheck
bool wxStringCheck(const wxString &val)
Allows extending a function with the signature:

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>