@stdobjects
::wxEmptyString
- @see @ref overview_string, @ref overview_unicode,
+ @see @ref overview_string, @ref overview_unicode,
@ref group_funcmacro_string "String-related functions", wxUString,
wxCharBuffer, wxUniChar, wxStringTokenizer, wxStringBuffer, wxStringBufferLength
*/
public:
/**
@name Standard types
-
+
Types used with wxString.
*/
//@{
a single character or a wide (Unicode) string. For all constructors (except the
default which creates an empty string) there is also a corresponding assignment
operator.
-
+
See also the assign() STL-like function.
*/
//@{
-
+
/**
Default constructor
*/
*/
wxString(const wxString& stringSrc);
+ /**
+ Construct a string consisting of @a nRepeat copies of ch.
+ */
+ wxString(wxUniChar ch, size_t nRepeat = 1);
+
+ /**
+ Construct a string consisting of @a nRepeat copies of ch.
+ */
+ wxString(wxUniCharRef ch, size_t nRepeat = 1);
+
+ /**
+ Construct a string consisting of @a nRepeat copies of ch
+ converted to Unicode using the current locale encoding.
+ */
+ wxString(char ch, size_t nRepeat = 1);
+
+ /**
+ Construct a string consisting of @a nRepeat copies of ch.
+ */
+ wxString(wchar_t ch, size_t nRepeat = 1);
/**
Constructs a string from the string literal @a psz using
/**
Constructs a string from @a str using the using the current locale encoding
to convert it to Unicode (wxConvLibc).
+
+ @see ToStdString()
*/
wxString(const std::string& str);
/**
Constructs a string from @a str.
+
+ @see ToStdWstring()
*/
wxString(const std::wstring& str);
-
+
/**
String destructor.
wxString operator =(wxUniChar c);
//@}
-
+
/**
These functions return the string length and/or check whether the string
is empty.
-
+
See also the length(), size() or empty() STL-like functions.
*/
//@{
-
+
/**
Returns the length of the string.
/**
@member_group_name{ch_access, Character access}
- Many functions below take a character index in the string.
- As with C strings and arrays, the indices start from 0, so the first character
+ Many functions below take a character index in the string.
+ As with C strings and arrays, the indices start from 0, so the first character
of a string is string[0]. An attempt to access a character beyond the end of the
string (which may even be 0 if the string is empty) will provoke an assert
failure in @ref overview_debugging "debug builds", but no checks are
/**
Returns a writable buffer of at least @a len bytes.
-
+
It returns a pointer to a new memory block, and the existing data will not be copied.
Call UngetWriteBuf() as soon as possible to put the string back into a reasonable state.
-
+
This method is deprecated, please use wxStringBuffer or wxStringBufferLength instead.
*/
wxStringCharType* GetWriteBuf(size_t len);
This method is deprecated, please use wxStringBuffer or wxStringBufferLength instead.
*/
void UngetWriteBuf();
-
+
/**
@overload
*/
void UngetWriteBuf(size_t len);
-
+
/**
Sets the character at position @e n.
*/
/**
Returns a the last character.
-
+
This is a wxWidgets 1.xx compatibility function;
you should not use it in new code.
*/
wxUniChar Last() const;
-
+
/**
Returns a reference to the last character (writable).
-
+
This is a wxWidgets 1.xx compatibility function;
you should not use it in new code.
*/
Returns a writable reference to the @a i-th character of the string.
*/
wxUniCharRef operator [](size_t i);
-
+
//@}
-
+
/**
@member_group_name{conv, Conversions}
-
+
This section contains both implicit and explicit conversions to C style
strings. Although implicit conversion is quite convenient, you are advised
to use wc_str() for the sake of clarity.
for file handling.
*/
const wchar_t* fn_str() const;
-
+
/**
@overload
*/
@see wc_str(), c_str(), mb_str()
*/
- const char* utf8_str() const;
-
- /**
- @overload
- */
- const wxCharBuffer utf8_str() const;
+ const wxScopedCharBuffer utf8_str() const;
/**
Converts the strings contents to the wide character represention
wchar_t* or UTF-8-encoded char*, depending on the build).
*/
const wxStringCharType *wx_str() const;
-
+
/**
Converts the string to an 8-bit string in ISO-8859-1 encoding in the
form of a wxCharBuffer (Unicode builds only).
const wxCharBuffer ToAscii() const;
/**
- Same as utf8_str().
+ Return the string as an std::string in current locale encoding.
+
+ Note that if the conversion of (Unicode) string contents to the current
+ locale fails, the return string will be empty. Be sure to check for
+ this to avoid silent data loss.
+
+ Instead of using this function it's also possible to write
+ @code
+ std::string s;
+ wxString wxs;
+ ...
+ s = std::string(wxs);
+ @endcode
+ but using ToStdString() may make the code more clear.
+
+ @since 2.9.1
*/
- const char* ToUTF8() const;
+ std::string ToStdString() const;
/**
- @overload
+ Return the string as an std::wstring.
+
+ Unlike ToStdString(), there is no danger of data loss when using this
+ function.
+
+ @since 2.9.1
+ */
+ std::wstring ToStdWstring() const;
+
+ /**
+ Same as utf8_str().
*/
- const wxCharBuffer ToUTF8() const;
+ const wxScopedCharBuffer ToUTF8() const;
//@}
@member_group_name{concat, Concatenation}
Almost anything may be concatenated (appended to) with a string!
-
- Note that the various operator<<() overloads work as C++ stream insertion
- operators. They insert the given value into the string.
+
+ Note that the various operator<<() overloads work as C++ stream insertion
+ operators. They insert the given value into the string.
Precision and format cannot be set using them. Use Printf() instead.
See also the insert() and append() STL-like functions.
Concatenation: returns a new string equal to the concatenation of the operands.
*/
wxString operator +(const wxString& x, const wxString& y);
-
+
/**
@overload
*/
wxString& operator<<(wchar_t ch);
wxString& operator<<(const wxCharBuffer& s);
wxString& operator<<(const wxWCharBuffer& s);
+ wxString& operator<<(wxUniChar ch);
wxString& operator<<(wxUniCharRef ch);
wxString& operator<<(unsigned int ui);
wxString& operator<<(long l);
Concatenation in place: the argument is appended to the string.
*/
void operator +=(const wxString& str);
-
+
/**
@overload
*/
void operator +=(wxUniChar c);
-
+
//@}
-
+
/**
@member_group_name{cmp, Comparison}
StartsWith() is helpful when parsing a line of text which should start
with some predefined prefix and is more efficient than doing direct string
comparison as you would also have to precalculate the length of the prefix.
-
+
See also the compare() STL-like function.
*/
//@{
int CmpNoCase(const wxString& s) const;
/**
- Test whether the string is equal to the single character @a c.
-
+ Test whether the string is equal to another string @a s.
+
The test is case-sensitive if @a caseSensitive is @true (default) or not if it is
@false.
-
- Returns @true if the string is equal to the character, @false otherwise.
-
+
+ @return @true if the string is equal to the other one, @false otherwise.
+
@see Cmp(), CmpNoCase()
*/
- bool IsSameAs(const wxString &s, bool caseSensitive = true) const;
-
+ bool IsSameAs(const wxString& s, bool caseSensitive = true) const;
+
/**
- @overload
+ Test whether the string is equal to the single character @a ch.
+
+ The test is case-sensitive if @a caseSensitive is @true (default) or not if it is
+ @false.
+
+ @return @true if the string is equal to this character, @false otherwise.
+
+ @see Cmp(), CmpNoCase()
*/
bool IsSameAs(wxUniChar ch, bool caseSensitive = true) const;
/**
This function can be used to test if the string starts with the specified
- @a prefix.
-
- If it does, the function will return @true and put the rest of the string
+ @a prefix.
+
+ If it does, the function will return @true and put the rest of the string
(i.e. after the prefix) into @a rest string if it is not @NULL.
Otherwise, the function returns @false and doesn't modify the @a rest.
*/
modify the @e rest.
*/
bool EndsWith(const wxString& suffix, wxString *rest = NULL) const;
-
+
//@}
-
-
+
+
/**
@member_group_name{substring, Substring extraction}
These functions allow you to extract a substring from the string. The
original string is not modified and the function returns the extracted
substring.
-
+
See also the at() and the substr() STL-like functions.
*/
/**
Returns the part of the string between the indices @a from and @a to
inclusive.
-
+
This is a wxWidgets 1.xx compatibility function, use Mid()
instead (but note that parameters have different meaning).
*/
wxString SubString(size_t from, size_t to) const;
-
+
/**
Same as Mid() (substring extraction).
*/
Returns the empty string if @a ch is not found.
*/
wxString BeforeLast(wxUniChar ch) const;
-
+
//@}
-
-
+
+
/**
@member_group_name{caseconv, Case conversion}
@see Upper()
*/
wxString& MakeUpper();
-
+
/**
Returns this string converted to upper case.
code.
*/
void UpperCase();
-
+
//@}
-
-
+
+
/**
@member_group_name{search, Searching and replacing}
These functions replace the standard @c strchr() and @c strstr()
functions.
-
+
See also the find(), rfind(), replace() STL-like functions.
*/
//@{
/**
- Searches for the given character @a ch.
+ Searches for the given character @a ch.
Returns the position or @c wxNOT_FOUND if not found.
*/
int Find(wxUniChar ch, bool fromEnd = false) const;
/**
- Searches for the given string @a sub.
+ Searches for the given string @a sub.
Returns the starting position or @c wxNOT_FOUND if not found.
*/
int Find(const wxString& sub) const;
/**
Same as Find().
-
+
This is a wxWidgets 1.xx compatibility function;
you should not use it in new code.
*/
/**
Same as Find().
-
+
This is a wxWidgets 1.xx compatibility function;
you should not use it in new code.
*/
/**
Replace first (or all) occurrences of substring with another one.
-
+
@param strOld
The string to search for replacing.
@param strNew
The substitution string.
@param replaceAll
- If @true a global replace will be done (default), otherwise only the
+ If @true a global replace will be done (default), otherwise only the
first occurrence will be replaced.
-
+
Returns the number of replacements made.
*/
size_t Replace(const wxString& strOld, const wxString& strNew,
@member_group_name{numconv, Conversion to numbers}
The string provides functions for conversion to signed and unsigned integer and
- floating point numbers. All functions take a pointer to the variable to
- put the numeric value in and return @true if the @b entire string could be
- converted to a number.
- */
+ floating point numbers.
+
+ All functions take a pointer to the variable to put the numeric value
+ in and return @true if the @b entire string could be converted to a
+ number. Notice if there is a valid number in the beginning of the
+ string, it is returned in the output parameter even if the function
+ returns @false because there is more text following it.
+ */
//@{
/**
- Attempts to convert the string to a floating point number. Returns @true on
- success (the number is stored in the location pointed to by @e val) or @false
- if the string does not represent such number (the value of @a val is not
- modified in this case).
+ Attempts to convert the string to a floating point number.
+
+ Returns @true on success (the number is stored in the location pointed to by
+ @a val) or @false if the string does not represent such number (the value of
+ @a val may still be modified in this case).
+
+ Note that unlike ToCDouble() this function uses a localized version of
+ @c wxStrtod() and thus needs as decimal point (and thousands separator) the
+ locale-specific decimal point. Thus you should use this function only when
+ you are sure that this string contains a floating point number formatted with
+ the rules of the locale currently in use (see wxLocale).
- @see ToLong(), ToULong()
+ Refer to the docs of the standard function @c strtod() for more details about
+ the supported syntax.
+
+ @see ToCDouble(), ToLong(), ToULong()
*/
bool ToDouble(double* val) const;
/**
- Attempts to convert the string to a signed integer in base @e base. Returns
- @true on success in which case the number is stored in the location
+ Variant of ToDouble() always working in "C" locale.
+
+ Works like ToDouble() but unlike it this function expects the floating point
+ number to be formatted always with the rules dictated by the "C" locale
+ (in particular, the decimal point must be a dot), independently from the
+ current application-wide locale (see wxLocale).
+
+ @see ToDouble(), ToLong(), ToULong()
+ */
+ bool ToCDouble(double* val) const;
+
+ /**
+ Attempts to convert the string to a signed integer in base @a base.
+
+ Returns @true on success in which case the number is stored in the location
pointed to by @a val or @false if the string does not represent a
- valid number in the given base (the value of @a val is not modified
- in this case).
+ valid number in the given base (the value of @a val may still be
+ modified in this case).
+
The value of @a base must be comprised between 2 and 36, inclusive, or
be a special value 0 which means that the usual rules of @c C numbers are
applied: if the number starts with @c 0x it is considered to be in base
which may have leading zeroes as they can yield unexpected (to the user not
familiar with C) results.
- @see ToDouble(), ToULong()
+ Note that unlike ToCLong() this function uses a localized version of
+ @c wxStrtol(). Thus you should use this function only when you are sure
+ that this string contains an integer number formatted with
+ the rules of the locale currently in use (see wxLocale).
+
+ Refer to the docs of the standard function @c strtol() for more details about
+ the supported syntax.
+
+ @see ToCDouble(), ToDouble(), ToULong()
*/
bool ToLong(long* val, int base = 10) const;
/**
- This is exactly the same as ToLong() but works with 64
- bit integer numbers.
+ Variant of ToLong() always working in "C" locale.
+
+ Works like ToLong() but unlike it this function expects the integer
+ number to be formatted always with the rules dictated by the "C" locale,
+ independently from the current application-wide locale (see wxLocale).
+
+ @see ToDouble(), ToLong(), ToULong()
+ */
+ bool ToCLong(long* val, int base = 10) const;
+
+ /**
+ This is exactly the same as ToLong() but works with 64 bit integer numbers.
+
Notice that currently it doesn't work (always returns @false) if parsing of 64
bit numbers is not supported by the underlying C run-time library. Compilers
with C99 support and Microsoft Visual C++ version 7 and higher do support this.
bool ToLongLong(wxLongLong_t* val, int base = 10) const;
/**
- Attempts to convert the string to an unsigned integer in base @e base.
+ Attempts to convert the string to an unsigned integer in base @a base.
+
Returns @true on success in which case the number is stored in the
location pointed to by @a val or @false if the string does not
- represent a valid number in the given base (the value of @a val is not
- modified in this case).
+ represent a valid number in the given base (the value of @a val may
+ still be modified in this case).
Please notice that this function behaves in the same way as the standard
@c strtoul() and so it simply converts negative numbers to unsigned
representation instead of rejecting them (e.g. -1 is returned as @c ULONG_MAX).
- See ToLong() for the more detailed description of the @a base parameter.
+ See ToLong() for the more detailed description of the @a base parameter
+ (and of the locale-specific behaviour of this function).
- @see ToDouble(), ToLong()
+ @see ToCULong(), ToDouble(), ToLong()
*/
bool ToULong(unsigned long* val, int base = 10) const;
/**
- This is exactly the same as ToULong() but works with 64
- bit integer numbers.
+ Variant of ToULong() always working in "C" locale.
+
+ Works like ToULong() but unlike it this function expects the integer
+ number to be formatted always with the rules dictated by the "C" locale,
+ independently from the current application-wide locale (see wxLocale).
+
+ @see ToDouble(), ToLong(), ToULong()
+ */
+ bool ToCULong(unsigned long* val, int base = 10) const;
+
+ /**
+ This is exactly the same as ToULong() but works with 64 bit integer
+ numbers.
+
Please see ToLongLong() for additional remarks.
*/
bool ToULongLong(wxULongLong_t* val, int base = 10) const;
@member_group_name{fmt, Formatting and printing}
Both formatted versions (Printf/() and stream-like insertion operators
- exist (for basic types only).
-
+ exist (for basic types only).
+
See also the static Format() and FormatV() functions.
*/
//@{
int PrintfV(const wxString& pszFormat, va_list argPtr);
//@}
-
-
+
+
/**
@member_group_name{mem, Memory management}
- The following are "advanced" functions and they will be needed rarely.
- Alloc() and Shrink() are only interesting for optimization purposes.
- wxStringBuffer and wxStringBufferLength classes may be very useful when working
+ The following are "advanced" functions and they will be needed rarely.
+ Alloc() and Shrink() are only interesting for optimization purposes.
+ wxStringBuffer and wxStringBufferLength classes may be very useful when working
with some external API which requires the caller to provide a writable buffer.
-
+
See also the reserve() and resize() STL-like functions.
*/
//@{
-
+
/**
Preallocate enough space for wxString to store @a nLen characters.
/**
Empties the string and frees memory occupied by it.
-
+
@see Empty()
*/
void Clear();
-
+
//@}
/**
Returns @true if target appears anywhere in wxString; else @false.
-
+
This is a wxWidgets 1.xx compatibility function; you should not use it in new code.
*/
bool Contains(const wxString& str) const;
/**
Makes the string empty, but doesn't free memory occupied by the string.
-
+
@see Clear().
*/
void Empty();
/**
Returns the number of occurrences of @e ch in the string.
-
+
This is a wxWidgets 1.xx compatibility function; you should not use it in new code.
*/
int Freq(wxUniChar ch) const;
/**
Returns @true if the string is an integer (with possible sign).
-
+
This is a wxWidgets 1.xx compatibility function; you should not use it in new code.
*/
bool IsNumber() const;
/**
Returns @true if the string is a word.
-
+
This is a wxWidgets 1.xx compatibility function; you should not use it in new code.
*/
bool IsWord() const;
/**
Adds @a count copies of @a chPad to the beginning, or to the end of the
string (the default).
-
+
Removes spaces from the left or from the right (default).
*/
wxString& Pad(size_t count, wxUniChar chPad = ' ', bool fromRight = true);
-
+
/**
Removes all characters from the string starting at @a pos.
Use Truncate() as a more readable alternative.
-
+
This is a wxWidgets 1.xx compatibility function; you should not use it in new code.
*/
wxString& Remove(size_t pos);
-
+
/**
Removes @a len characters from the string, starting at @a pos.
-
+
This is a wxWidgets 1.xx compatibility function; you should not use it in new code.
*/
wxString& Remove(size_t pos, size_t len);
wxString& RemoveLast(size_t n = 1);
/**
- Strip characters at the front and/or end.
-
+ Strip characters at the front and/or end.
+
This is the same as Trim() except that it doesn't change this string.
-
+
This is a wxWidgets 1.xx compatibility function; you should not use it in new code.
*/
wxString Strip(stripType s = trailing) const;
Truncate the string to the given length.
*/
wxString& Truncate(size_t len);
-
+
//@}
@member_group_name{iter, Iterator interface}
These methods return iterators to the beginnnig or end of the string.
-
+
Please see any STL reference (e.g. http://www.cppreference.com/wiki/string/start)
for their documentation.
*/
//@{
-
+
const_iterator begin() const;
iterator begin();
const_iterator end() const;
reverse_iterator rbegin();
const_reverse_iterator rend() const;
reverse_iterator rend();
-
+
//@}
/**
@member_group_name{stl, STL interface}
- The supported STL functions are listed here.
-
+ The supported STL functions are listed here.
+
Please see any STL reference (e.g. http://www.cppreference.com/wiki/string/start)
for their documentation.
*/
//@{
-
+
wxString& append(const wxString& str, size_t pos, size_t n);
wxString& append(const wxString& str);
wxString& append(const char *sz, size_t n);
size_type size() const;
wxString substr(size_t nStart = 0, size_t nLen = npos) const;
void swap(wxString& str);
-
+
//@}
-
+
// STATIC FUNCTIONS
@code
wxString theAnswer;
- wxStringBuffer theAnswerBuffer(theAnswer, 1024);
+ wxStringBufferLength theAnswerBuffer(theAnswer, 1024);
int nLength = GetMeaningOfLifeAsString(theAnswerBuffer);
theAnswerBuffer.SetLength(nLength);
if ( theAnswer != "42" )
wxLogError("Something is very wrong!");
@endcode
- @todo
- the example above does not make use of wxStringBufferLength??
-
Note that the exact usage of this depends on whether or not wxUSE_STL is
enabled. If wxUSE_STL is enabled, wxStringBuffer creates a separate empty
character buffer, and if wxUSE_STL is disabled, it uses GetWriteBuf() from