1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: interface of wxStringBuffer, wxString
4 // Author: wxWidgets team
6 // Licence: wxWindows license
7 /////////////////////////////////////////////////////////////////////////////
13 The wxString class has been completely rewritten for wxWidgets 3.0
14 and this change was actually the main reason for the calling that
15 version wxWidgets 3.0.
17 wxString is a class representing a Unicode character string.
18 wxString uses @c std::basic_string internally (even if @c wxUSE_STL is not defined)
19 to store its content (unless this is not supported by the compiler or disabled
20 specifically when building wxWidgets) and it therefore inherits
21 many features from @c std::basic_string. (Note that most implementations of
22 @c std::basic_string are thread-safe and don't use reference counting.)
24 These @c std::basic_string standard functions are only listed here, but
25 they are not fully documented in this manual; see the STL documentation
26 (http://www.cppreference.com/wiki/string/start) for more info.
27 The behaviour of all these functions is identical to the behaviour
30 You may notice that wxString sometimes has several functions which do
31 the same thing like Length(), Len() and length() which all return the
32 string length. In all cases of such duplication the @c std::string
33 compatible methods should be used.
35 For informations about the internal encoding used by wxString and
36 for important warnings and advices for using it, please read
37 the @ref overview_string.
39 Since wxWidgets 3.0 wxString always stores Unicode strings, so you should
40 be sure to read also @ref overview_unicode.
43 @section string_index Index of the member groups
45 Links for quick access to the various categories of wxString functions:
46 - @ref_member_group{ctor, Constructors and assignment operators}
47 - @ref_member_group{length, Length functions}
48 - @ref_member_group{ch_access, Character access functions}
49 - @ref_member_group{conv, Conversions functions}
50 - @ref_member_group{concat, Concatenation functions}
51 - @ref_member_group{cmp, Comparison functions}
52 - @ref_member_group{substring, Substring extraction functions}
53 - @ref_member_group{caseconv, Case conversion functions}
54 - @ref_member_group{search, Searching and replacing functions}
55 - @ref_member_group{numconv, Conversion to numbers functions}
56 - @ref_member_group{fmt, Formatting and printing functions}
57 - @ref_member_group{mem, Memory management functions}
58 - @ref_member_group{misc, Miscellaneous functions}
59 - @ref_member_group{iter, Iterator interface functions}
60 - @ref_member_group{stl, STL interface functions}
69 @see @ref overview_string, @ref overview_unicode,
70 @ref group_funcmacro_string "String-related functions", wxUString,
71 wxCharBuffer, wxUniChar, wxStringTokenizer, wxStringBuffer, wxStringBufferLength
79 Types used with wxString.
82 typedef wxUniChar value_type
;
83 typedef wxUniChar char_type
;
84 typedef wxUniCharRef reference
;
85 typedef wxChar
* pointer
;
86 typedef const wxChar
* const_pointer
;
87 typedef size_t size_type
;
88 typedef wxUniChar const_reference
;
93 @member_group_name{ctor, Constructors and assignment operators}
95 A string may be constructed either from a C string, (some number of copies of)
96 a single character or a wide (Unicode) string. For all constructors (except the
97 default which creates an empty string) there is also a corresponding assignment
100 See also the assign() STL-like function.
110 Creates a string from another string.
111 Just increases the ref count by 1.
113 wxString(const wxString
& stringSrc
);
117 Constructs a string from the string literal @a psz using
118 the current locale encoding to convert it to Unicode (wxConvLibc).
120 wxString(const char *psz
);
123 Constructs a string from the string literal @a psz using
124 @a conv to convert it Unicode.
126 wxString(const char *psz
, const wxMBConv
& conv
);
129 Constructs a string from the first @a nLength character of the string literal @a psz using
130 the current locale encoding to convert it to Unicode (wxConvLibc).
132 wxString(const char *psz
, size_t nLength
);
135 Constructs a string from the first @a nLength character of the string literal @a psz using
136 @a conv to convert it Unicode.
138 wxString(const char *psz
, const wxMBConv
& conv
, size_t nLength
);
141 Constructs a string from the string literal @a pwz.
143 wxString(const wchar_t *pwz
);
146 Constructs a string from the first @a nLength characters of the string literal @a pwz.
148 wxString(const wchar_t *pwz
, size_t nLength
);
151 Constructs a string from @a buf using the using the current locale
152 encoding to convert it to Unicode.
154 wxString(const wxCharBuffer
& buf
);
157 Constructs a string from @a buf.
159 wxString(const wxWCharBuffer
& buf
);
162 Constructs a string from @a str using the using the current locale encoding
163 to convert it to Unicode (wxConvLibc).
165 wxString(const std::string
& str
);
168 Constructs a string from @a str.
170 wxString(const std::wstring
& str
);
175 Note that this is not virtual, so wxString must not be inherited from.
180 Assignment: see the relative wxString constructor.
182 wxString
operator =(const wxString
& str
);
185 Assignment: see the relative wxString constructor.
187 wxString
operator =(wxUniChar c
);
194 @member_group_name{length, String length}
196 These functions return the string length and/or check whether the string
199 See also the length(), size() or empty() STL-like functions.
205 Returns the length of the string.
210 Returns the length of the string (same as Len).
211 This is a wxWidgets 1.xx compatibility function; you should not use it in new
214 size_t Length() const;
217 Returns @true if the string is empty.
219 bool IsEmpty() const;
222 Returns @true if the string is empty (same as wxString::IsEmpty).
223 This is a wxWidgets 1.xx compatibility function; you should not use it in new
229 Empty string is @false, so !string will only return @true if the
234 bool operator!() const;
241 @member_group_name{ch_access, Character access}
243 Many functions below take a character index in the string.
244 As with C strings and arrays, the indices start from 0, so the first character
245 of a string is string[0]. An attempt to access a character beyond the end of the
246 string (which may even be 0 if the string is empty) will provoke an assert
247 failure in @ref overview_debugging "debug builds", but no checks are
248 done in release builds.
253 Returns the character at position @a n (read-only).
255 wxUniChar
GetChar(size_t n
) const;
258 wxWidgets compatibility conversion. Same as c_str().
260 const wxCStrData
GetData() const;
263 Returns a reference to the character at position @a n.
265 wxUniCharRef
GetWritableChar(size_t n
);
268 Returns a writable buffer of at least @a len bytes.
270 It returns a pointer to a new memory block, and the existing data will not be copied.
271 Call UngetWriteBuf() as soon as possible to put the string back into a reasonable state.
273 This method is deprecated, please use wxStringBuffer or wxStringBufferLength instead.
275 wxStringCharType
* GetWriteBuf(size_t len
);
278 Puts the string back into a reasonable state (in which it can be used
279 normally), after GetWriteBuf() was called.
281 The version of the function without the @a len parameter will calculate the
282 new string length itself assuming that the string is terminated by the first
283 @c NUL character in it while the second one will use the specified length
284 and thus is the only version which should be used with the strings with
285 embedded @c NULs (it is also slightly more efficient as @c strlen()
286 doesn't have to be called).
288 This method is deprecated, please use wxStringBuffer or wxStringBufferLength instead.
290 void UngetWriteBuf();
295 void UngetWriteBuf(size_t len
);
298 Sets the character at position @e n.
300 void SetChar(size_t n
, wxUniChar ch
);
303 Returns a the last character.
305 This is a wxWidgets 1.xx compatibility function;
306 you should not use it in new code.
308 wxUniChar
Last() const;
311 Returns a reference to the last character (writable).
313 This is a wxWidgets 1.xx compatibility function;
314 you should not use it in new code.
319 Returns the @a i-th character of the string.
321 wxUniChar
operator [](size_t i
) const;
324 Returns a writable reference to the @a i-th character of the string.
326 wxUniCharRef
operator [](size_t i
);
332 @member_group_name{conv, Conversions}
334 This section contains both implicit and explicit conversions to C style
335 strings. Although implicit conversion is quite convenient, you are advised
336 to use wc_str() for the sake of clarity.
341 Returns a lightweight intermediate class which is in turn implicitly
342 convertible to both @c const @c char* and to @c const @c wchar_t*.
343 Given this ambiguity it is mostly better to use wc_str(), mb_str() or
346 Please see the @ref overview_unicode for more information about it.
348 Note that the returned value is not convertible to @c char* or
349 @c wchar_t*, use char_str() or wchar_str() if you need to pass
350 string value to a function expecting non-const pointer.
352 @see wc_str(), utf8_str(), c_str(), mb_str(), fn_str()
354 wxCStrData
c_str() const;
357 Returns an object with string data that is implicitly convertible to
358 @c char* pointer. Note that any change to the returned buffer is lost and so
359 this function is only usable for passing strings to legacy libraries that
360 don't have const-correct API. Use wxStringBuffer if you want to modify
365 wxWritableCharBuffer
char_str(const wxMBConv
& conv
= wxConvLibc
) const;
368 Returns buffer of the specified type containing the string data.
370 This method is only useful in template code, otherwise you should
371 directly call mb_str() or wc_str() if you need to retrieve a narrow or
372 wide string from this wxString. The template parameter @a t should be
373 either @c char or @c wchar_t.
375 Notice that retrieving a char buffer in UTF-8 build will return the
376 internal string representation in UTF-8 while in wchar_t build the char
377 buffer will contain the conversion of the string to the encoding of the
378 current locale (and so can fail).
381 If non-@NULL, filled with the length of the returned buffer.
384 buffer containing the string contents in the specified type,
385 notice that it may be @NULL if the conversion failed (e.g. Unicode
386 string couldn't be converted to the current encoding when @a T is
389 template <typename T
>
390 wxCharTypeBuffer
<T
> tchar_str(size_t *len
= NULL
) const;
393 Returns a string representation suitable for passing to OS' functions
396 const wchar_t* fn_str() const;
401 const char* fn_str() const;
406 const wxCharBuffer
fn_str() const;
409 Returns the multibyte (C string) representation of the string
410 using @e conv's wxMBConv::cWC2MB method and returns wxCharBuffer.
412 @see wc_str(), utf8_str(), c_str(), wxMBConv
414 const wxCharBuffer
mb_str(const wxMBConv
& conv
= wxConvLibc
) const;
417 Converts the strings contents to UTF-8 and returns it either as a
418 temporary wxCharBuffer object or as a pointer to the internal
419 string contents in UTF-8 build.
421 @see wc_str(), c_str(), mb_str()
423 const wxScopedCharBuffer
utf8_str() const;
426 Converts the strings contents to the wide character represention
427 and returns it as a temporary wxWCharBuffer object (Unix and OS X)
428 or returns a pointer to the internal string contents in wide character
431 The macro wxWX2WCbuf is defined as the correct return type (without const).
433 @see utf8_str(), c_str(), mb_str(), fn_str(), wchar_str()
435 const wchar_t* wc_str() const;
440 const wxWCharBuffer
wc_str() const;
443 Returns an object with string data that is implicitly convertible to
444 @c char* pointer. Note that changes to the returned buffer may or may
445 not be lost (depending on the build) and so this function is only usable for
446 passing strings to legacy libraries that don't have const-correct API. Use
447 wxStringBuffer if you want to modify the string.
449 @see mb_str(), wc_str(), fn_str(), c_str(), char_str()
451 wxWritableWCharBuffer
wchar_str() const;
454 Explicit conversion to C string in the internal representation (either
455 wchar_t* or UTF-8-encoded char*, depending on the build).
457 const wxStringCharType
*wx_str() const;
460 Converts the string to an 8-bit string in ISO-8859-1 encoding in the
461 form of a wxCharBuffer (Unicode builds only).
463 This is a convenience method useful when storing binary data in
464 wxString. It should be used @em only for this purpose. It is only valid
465 to call this method on strings created using From8BitData().
469 @see wxString::From8BitData()
471 const char* To8BitData() const;
476 const wxCharBuffer
To8BitData() const;
479 Converts the string to an ASCII, 7-bit string in the form of
480 a wxCharBuffer (Unicode builds only) or a C string (ANSI builds).
481 Note that this conversion only works if the string contains only ASCII
482 characters. The @ref mb_str() "mb_str" method provides more
483 powerful means of converting wxString to C string.
485 const char* ToAscii() const;
490 const wxCharBuffer
ToAscii() const;
495 const wxScopedCharBuffer
ToUTF8() const;
501 @member_group_name{concat, Concatenation}
503 Almost anything may be concatenated (appended to) with a string!
505 Note that the various operator<<() overloads work as C++ stream insertion
506 operators. They insert the given value into the string.
507 Precision and format cannot be set using them. Use Printf() instead.
509 See also the insert() and append() STL-like functions.
514 Appends the string literal @a psz.
516 wxString
& Append(const char* psz
);
519 Appends the wide string literal @a pwz.
521 wxString
& Append(const wchar_t* pwz
);
524 Appends the string literal @a psz with max length @a nLen.
526 wxString
& Append(const char* psz
, size_t nLen
);
529 Appends the wide string literal @a psz with max length @a nLen.
531 wxString
& Append(const wchar_t* pwz
, size_t nLen
);
534 Appends the string @a s.
536 wxString
& Append(const wxString
& s
);
539 Appends the character @a ch @a count times.
541 wxString
&Append(wxUniChar ch
, size_t count
= 1u);
544 Prepends @a str to this string, returning a reference to this string.
546 wxString
& Prepend(const wxString
& str
);
549 Concatenation: returns a new string equal to the concatenation of the operands.
551 wxString
operator +(const wxString
& x
, const wxString
& y
);
556 wxString
operator +(const wxString
& x
, wxUniChar y
);
558 wxString
& operator<<(const wxString
& s
);
559 wxString
& operator<<(const char* psz
);
560 wxString
& operator<<(const wchar_t* pwz
);
561 wxString
& operator<<(const wxCStrData
& psz
);
562 wxString
& operator<<(char ch
);
563 wxString
& operator<<(unsigned char ch
);
564 wxString
& operator<<(wchar_t ch
);
565 wxString
& operator<<(const wxCharBuffer
& s
);
566 wxString
& operator<<(const wxWCharBuffer
& s
);
567 wxString
& operator<<(wxUniCharRef ch
);
568 wxString
& operator<<(unsigned int ui
);
569 wxString
& operator<<(long l
);
570 wxString
& operator<<(unsigned long ul
);
571 wxString
& operator<<(wxLongLong_t ll
);
572 wxString
& operator<<(wxULongLong_t ul
);
573 wxString
& operator<<(float f
);
574 wxString
& operator<<(double d
);
577 Concatenation in place: the argument is appended to the string.
579 void operator +=(const wxString
& str
);
584 void operator +=(wxUniChar c
);
590 @member_group_name{cmp, Comparison}
592 The default comparison function Cmp() is case-sensitive and so is the default
593 version of IsSameAs(). For case insensitive comparisons you should use CmpNoCase()
594 or give a second parameter to IsSameAs(). This last function is maybe more
595 convenient if only equality of the strings matters because it returns a boolean
596 @true value if the strings are the same and not 0 (which is usually @false
599 Matches() is a poor man's regular expression matcher: it only understands
600 '*' and '?' metacharacters in the sense of DOS command line interpreter.
602 StartsWith() is helpful when parsing a line of text which should start
603 with some predefined prefix and is more efficient than doing direct string
604 comparison as you would also have to precalculate the length of the prefix.
606 See also the compare() STL-like function.
611 Case-sensitive comparison.
612 Returns a positive value if the string is greater than the argument,
613 zero if it is equal to it or a negative value if it is less than the
614 argument (same semantics as the standard @c strcmp() function).
616 @see CmpNoCase(), IsSameAs().
618 int Cmp(const wxString
& s
) const;
621 Case-insensitive comparison.
622 Returns a positive value if the string is greater than the argument,
623 zero if it is equal to it or a negative value if it is less than the
624 argument (same semantics as the standard @c strcmp() function).
626 @see Cmp(), IsSameAs().
628 int CmpNoCase(const wxString
& s
) const;
631 Test whether the string is equal to another string @a s.
633 The test is case-sensitive if @a caseSensitive is @true (default) or not if it is
636 @return @true if the string is equal to the other one, @false otherwise.
638 @see Cmp(), CmpNoCase()
640 bool IsSameAs(const wxString
& s
, bool caseSensitive
= true) const;
643 Test whether the string is equal to the single character @a ch.
645 The test is case-sensitive if @a caseSensitive is @true (default) or not if it is
648 @return @true if the string is equal to this character, @false otherwise.
650 @see Cmp(), CmpNoCase()
652 bool IsSameAs(wxUniChar ch
, bool caseSensitive
= true) const;
655 Returns @true if the string contents matches a mask containing '*' and '?'.
657 bool Matches(const wxString
& mask
) const;
660 This function can be used to test if the string starts with the specified
663 If it does, the function will return @true and put the rest of the string
664 (i.e. after the prefix) into @a rest string if it is not @NULL.
665 Otherwise, the function returns @false and doesn't modify the @a rest.
667 bool StartsWith(const wxString
& prefix
, wxString
*rest
= NULL
) const;
670 This function can be used to test if the string ends with the specified
671 @e suffix. If it does, the function will return @true and put the
672 beginning of the string before the suffix into @e rest string if it is not
673 @NULL. Otherwise, the function returns @false and doesn't
676 bool EndsWith(const wxString
& suffix
, wxString
*rest
= NULL
) const;
682 @member_group_name{substring, Substring extraction}
684 These functions allow you to extract a substring from the string. The
685 original string is not modified and the function returns the extracted
688 See also the at() and the substr() STL-like functions.
692 Returns a substring starting at @e first, with length @e count, or the rest of
693 the string if @a count is the default value.
695 wxString
Mid(size_t first
, size_t nCount
= wxString::npos
) const;
698 Returns the part of the string between the indices @a from and @a to
701 This is a wxWidgets 1.xx compatibility function, use Mid()
702 instead (but note that parameters have different meaning).
704 wxString
SubString(size_t from
, size_t to
) const;
707 Same as Mid() (substring extraction).
709 wxString
operator()(size_t start
, size_t len
) const;
712 Returns the first @a count characters of the string.
714 wxString
Left(size_t count
) const;
717 Returns the last @a count characters.
719 wxString
Right(size_t count
) const;
722 Gets all the characters after the first occurrence of @e ch.
723 Returns the empty string if @e ch is not found.
725 wxString
AfterFirst(wxUniChar ch
) const;
728 Gets all the characters after the last occurrence of @e ch.
729 Returns the whole string if @e ch is not found.
731 wxString
AfterLast(wxUniChar ch
) const;
734 Gets all characters before the first occurrence of @e ch.
735 Returns the whole string if @a ch is not found.
737 wxString
BeforeFirst(wxUniChar ch
) const;
740 Gets all characters before the last occurrence of @e ch.
741 Returns the empty string if @a ch is not found.
743 wxString
BeforeLast(wxUniChar ch
) const;
749 @member_group_name{caseconv, Case conversion}
751 The MakeXXX() variants modify the string in place, while the other functions
752 return a new string which contains the original text converted to the upper or
753 lower case and leave the original string unchanged.
758 Return the copy of the string with the first string character in the
759 upper case and the subsequent ones in the lower case.
763 @see MakeCapitalized()
765 wxString
Capitalize() const;
768 Returns this string converted to the lower case.
772 wxString
Lower() const;
776 This is a wxWidgets 1.xx compatibility function; you should not use it in new
782 Converts the first characters of the string to the upper case and all
783 the subsequent ones to the lower case and returns the result.
789 wxString
& MakeCapitalized();
792 Converts all characters to lower case and returns the reference to the
797 wxString
& MakeLower();
800 Converts all characters to upper case and returns the reference to the
805 wxString
& MakeUpper();
808 Returns this string converted to upper case.
812 wxString
Upper() const;
815 The same as MakeUpper().
817 This is a wxWidgets 1.xx compatibility function; you should not use it in new
826 @member_group_name{search, Searching and replacing}
828 These functions replace the standard @c strchr() and @c strstr()
831 See also the find(), rfind(), replace() STL-like functions.
836 Searches for the given character @a ch.
837 Returns the position or @c wxNOT_FOUND if not found.
839 int Find(wxUniChar ch
, bool fromEnd
= false) const;
842 Searches for the given string @a sub.
843 Returns the starting position or @c wxNOT_FOUND if not found.
845 int Find(const wxString
& sub
) const;
850 This is a wxWidgets 1.xx compatibility function;
851 you should not use it in new code.
853 int First(wxUniChar ch
) const;
858 This is a wxWidgets 1.xx compatibility function;
859 you should not use it in new code.
861 int First(const wxString
& str
) const;
864 Replace first (or all) occurrences of substring with another one.
867 The string to search for replacing.
869 The substitution string.
871 If @true a global replace will be done (default), otherwise only the
872 first occurrence will be replaced.
874 Returns the number of replacements made.
876 size_t Replace(const wxString
& strOld
, const wxString
& strNew
,
877 bool replaceAll
= true);
884 @member_group_name{numconv, Conversion to numbers}
886 The string provides functions for conversion to signed and unsigned integer and
887 floating point numbers.
889 All functions take a pointer to the variable to put the numeric value
890 in and return @true if the @b entire string could be converted to a
891 number. Notice if there is a valid number in the beginning of the
892 string, it is returned in the output parameter even if the function
893 returns @false because there is more text following it.
898 Attempts to convert the string to a floating point number.
900 Returns @true on success (the number is stored in the location pointed to by
901 @a val) or @false if the string does not represent such number (the value of
902 @a val may still be modified in this case).
904 Note that unlike ToCDouble() this function uses a localized version of
905 @c wxStrtod() and thus needs as decimal point (and thousands separator) the
906 locale-specific decimal point. Thus you should use this function only when
907 you are sure that this string contains a floating point number formatted with
908 the rules of the locale currently in use (see wxLocale).
910 Refer to the docs of the standard function @c strtod() for more details about
911 the supported syntax.
913 @see ToCDouble(), ToLong(), ToULong()
915 bool ToDouble(double* val
) const;
918 Variant of ToDouble() always working in "C" locale.
920 Works like ToDouble() but unlike it this function expects the floating point
921 number to be formatted always with the rules dictated by the "C" locale
922 (in particular, the decimal point must be a dot), independently from the
923 current application-wide locale (see wxLocale).
925 @see ToDouble(), ToLong(), ToULong()
927 bool ToCDouble(double* val
) const;
930 Attempts to convert the string to a signed integer in base @a base.
932 Returns @true on success in which case the number is stored in the location
933 pointed to by @a val or @false if the string does not represent a
934 valid number in the given base (the value of @a val may still be
935 modified in this case).
937 The value of @a base must be comprised between 2 and 36, inclusive, or
938 be a special value 0 which means that the usual rules of @c C numbers are
939 applied: if the number starts with @c 0x it is considered to be in base
940 16, if it starts with @c 0 - in base 8 and in base 10 otherwise. Note
941 that you may not want to specify the base 0 if you are parsing the numbers
942 which may have leading zeroes as they can yield unexpected (to the user not
943 familiar with C) results.
945 Note that unlike ToCLong() this function uses a localized version of
946 @c wxStrtol(). Thus you should use this function only when you are sure
947 that this string contains an integer number formatted with
948 the rules of the locale currently in use (see wxLocale).
950 Refer to the docs of the standard function @c strtol() for more details about
951 the supported syntax.
953 @see ToCDouble(), ToDouble(), ToULong()
955 bool ToLong(long* val
, int base
= 10) const;
958 Variant of ToLong() always working in "C" locale.
960 Works like ToLong() but unlike it this function expects the integer
961 number to be formatted always with the rules dictated by the "C" locale,
962 independently from the current application-wide locale (see wxLocale).
964 @see ToDouble(), ToLong(), ToULong()
966 bool ToCLong(long* val
, int base
= 10) const;
969 This is exactly the same as ToLong() but works with 64 bit integer numbers.
971 Notice that currently it doesn't work (always returns @false) if parsing of 64
972 bit numbers is not supported by the underlying C run-time library. Compilers
973 with C99 support and Microsoft Visual C++ version 7 and higher do support this.
975 @see ToLong(), ToULongLong()
977 bool ToLongLong(wxLongLong_t
* val
, int base
= 10) const;
980 Attempts to convert the string to an unsigned integer in base @a base.
982 Returns @true on success in which case the number is stored in the
983 location pointed to by @a val or @false if the string does not
984 represent a valid number in the given base (the value of @a val may
985 still be modified in this case).
987 Please notice that this function behaves in the same way as the standard
988 @c strtoul() and so it simply converts negative numbers to unsigned
989 representation instead of rejecting them (e.g. -1 is returned as @c ULONG_MAX).
991 See ToLong() for the more detailed description of the @a base parameter
992 (and of the locale-specific behaviour of this function).
994 @see ToCULong(), ToDouble(), ToLong()
996 bool ToULong(unsigned long* val
, int base
= 10) const;
999 Variant of ToULong() always working in "C" locale.
1001 Works like ToULong() but unlike it this function expects the integer
1002 number to be formatted always with the rules dictated by the "C" locale,
1003 independently from the current application-wide locale (see wxLocale).
1005 @see ToDouble(), ToLong(), ToULong()
1007 bool ToCULong(unsigned long* val
, int base
= 10) const;
1010 This is exactly the same as ToULong() but works with 64 bit integer
1013 Please see ToLongLong() for additional remarks.
1015 bool ToULongLong(wxULongLong_t
* val
, int base
= 10) const;
1021 @member_group_name{fmt, Formatting and printing}
1023 Both formatted versions (Printf/() and stream-like insertion operators
1024 exist (for basic types only).
1026 See also the static Format() and FormatV() functions.
1031 Similar to the standard function @e sprintf(). Returns the number of
1032 characters written, or an integer less than zero on error.
1033 Note that if @c wxUSE_PRINTF_POS_PARAMS is set to 1, then this function supports
1034 Unix98-style positional parameters:
1036 @note This function will use a safe version of @e vsprintf() (usually called
1037 @e vsnprintf()) whenever available to always allocate the buffer of correct
1038 size. Unfortunately, this function is not available on all platforms and the
1039 dangerous @e vsprintf() will be used then which may lead to buffer overflows.
1041 int Printf(const wxString
& pszFormat
, ...);
1044 Similar to vprintf. Returns the number of characters written, or an integer
1048 int PrintfV(const wxString
& pszFormat
, va_list argPtr
);
1054 @member_group_name{mem, Memory management}
1056 The following are "advanced" functions and they will be needed rarely.
1057 Alloc() and Shrink() are only interesting for optimization purposes.
1058 wxStringBuffer and wxStringBufferLength classes may be very useful when working
1059 with some external API which requires the caller to provide a writable buffer.
1061 See also the reserve() and resize() STL-like functions.
1066 Preallocate enough space for wxString to store @a nLen characters.
1068 Please note that this method does the same thing as the standard
1069 reserve() one and shouldn't be used in new code.
1071 This function may be used to increase speed when the string is
1072 constructed by repeated concatenation as in
1075 // delete all vowels from the string
1076 wxString DeleteAllVowels(const wxString& original)
1080 size_t len = original.length();
1084 for ( size_t n = 0; n < len; n++ )
1086 if ( strchr("aeuio", tolower(original[n])) == NULL )
1087 result += original[n];
1094 because it will avoid the need to reallocate string memory many times
1095 (in case of long strings). Note that it does not set the maximal length
1096 of a string -- it will still expand if more than @a nLen characters are
1097 stored in it. Also, it does not truncate the existing string (use
1098 Truncate() for this) even if its current length is greater than @a nLen.
1100 @return @true if memory was successfully allocated, @false otherwise.
1102 bool Alloc(size_t nLen
);
1105 Minimizes the string's memory. This can be useful after a call to
1106 Alloc() if too much memory were preallocated.
1111 Returns a deep copy of the string.
1113 That is, the returned string is guaranteed to not share data with this
1114 string when using reference-counted wxString implementation.
1116 This method is primarily useful for passing strings between threads
1117 (because wxString is not thread-safe). Unlike creating a copy using
1118 @c wxString(c_str()), Clone() handles embedded NULs correctly.
1122 wxString
Clone() const;
1125 Empties the string and frees memory occupied by it.
1136 @member_group_name{misc, Miscellaneous}
1138 Miscellaneous other string functions.
1143 Returns @true if target appears anywhere in wxString; else @false.
1145 This is a wxWidgets 1.xx compatibility function; you should not use it in new code.
1147 bool Contains(const wxString
& str
) const;
1150 Makes the string empty, but doesn't free memory occupied by the string.
1157 Returns the number of occurrences of @e ch in the string.
1159 This is a wxWidgets 1.xx compatibility function; you should not use it in new code.
1161 int Freq(wxUniChar ch
) const;
1164 Returns @true if the string contains only ASCII characters.
1165 See wxUniChar::IsAscii for more details.
1167 This is a wxWidgets 1.xx compatibility function; you should not use it in new
1170 bool IsAscii() const;
1173 Returns @true if the string is an integer (with possible sign).
1175 This is a wxWidgets 1.xx compatibility function; you should not use it in new code.
1177 bool IsNumber() const;
1180 Returns @true if the string is a word.
1182 This is a wxWidgets 1.xx compatibility function; you should not use it in new code.
1184 bool IsWord() const;
1187 Adds @a count copies of @a chPad to the beginning, or to the end of the
1188 string (the default).
1190 Removes spaces from the left or from the right (default).
1192 wxString
& Pad(size_t count
, wxUniChar chPad
= ' ', bool fromRight
= true);
1195 Removes all characters from the string starting at @a pos.
1196 Use Truncate() as a more readable alternative.
1198 This is a wxWidgets 1.xx compatibility function; you should not use it in new code.
1200 wxString
& Remove(size_t pos
);
1203 Removes @a len characters from the string, starting at @a pos.
1205 This is a wxWidgets 1.xx compatibility function; you should not use it in new code.
1207 wxString
& Remove(size_t pos
, size_t len
);
1210 Removes the last character.
1212 wxString
& RemoveLast(size_t n
= 1);
1215 Strip characters at the front and/or end.
1217 This is the same as Trim() except that it doesn't change this string.
1219 This is a wxWidgets 1.xx compatibility function; you should not use it in new code.
1221 wxString
Strip(stripType s
= trailing
) const;
1224 Removes white-space (space, tabs, form feed, newline and carriage return) from
1225 the left or from the right end of the string (right is default).
1227 wxString
& Trim(bool fromRight
= true);
1230 Truncate the string to the given length.
1232 wxString
& Truncate(size_t len
);
1240 @member_group_name{iter, Iterator interface}
1242 These methods return iterators to the beginnnig or end of the string.
1244 Please see any STL reference (e.g. http://www.cppreference.com/wiki/string/start)
1245 for their documentation.
1249 const_iterator
begin() const;
1251 const_iterator
end() const;
1254 const_reverse_iterator
rbegin() const;
1255 reverse_iterator
rbegin();
1256 const_reverse_iterator
rend() const;
1257 reverse_iterator
rend();
1264 @member_group_name{stl, STL interface}
1266 The supported STL functions are listed here.
1268 Please see any STL reference (e.g. http://www.cppreference.com/wiki/string/start)
1269 for their documentation.
1273 wxString
& append(const wxString
& str
, size_t pos
, size_t n
);
1274 wxString
& append(const wxString
& str
);
1275 wxString
& append(const char *sz
, size_t n
);
1276 wxString
& append(const wchar_t *sz
, size_t n
);
1277 wxString
& append(size_t n
, wxUniChar ch
);
1278 wxString
& append(const_iterator first
, const_iterator last
);
1280 wxString
& assign(const wxString
& str
, size_t pos
, size_t n
);
1281 wxString
& assign(const wxString
& str
);
1282 wxString
& assign(const char *sz
, size_t n
);
1283 wxString
& assign(const wchar_t *sz
, size_t n
);
1284 wxString
& assign(size_t n
, wxUniChar ch
);
1285 wxString
& assign(const_iterator first
, const_iterator last
);
1287 wxUniChar
at(size_t n
) const;
1288 wxUniCharRef
at(size_t n
);
1292 size_type
capacity() const;
1294 int compare(const wxString
& str
) const;
1295 int compare(size_t nStart
, size_t nLen
, const wxString
& str
) const;
1296 int compare(size_t nStart
, size_t nLen
,
1297 const wxString
& str
, size_t nStart2
, size_t nLen2
) const;
1298 int compare(size_t nStart
, size_t nLen
,
1299 const char* sz
, size_t nCount
= npos
) const;
1300 int compare(size_t nStart
, size_t nLen
,
1301 const wchar_t* sz
, size_t nCount
= npos
) const;
1303 wxCStrData
data() const;
1307 wxString
& erase(size_type pos
= 0, size_type n
= npos
);
1308 iterator
erase(iterator first
, iterator last
);
1309 iterator
erase(iterator first
);
1311 size_t find(const wxString
& str
, size_t nStart
= 0) const;
1312 size_t find(const char* sz
, size_t nStart
= 0, size_t n
= npos
) const;
1313 size_t find(const wchar_t* sz
, size_t nStart
= 0, size_t n
= npos
) const;
1314 size_t find(wxUniChar ch
, size_t nStart
= 0) const;
1315 size_t find_first_of(const char* sz
, size_t nStart
= 0) const;
1316 size_t find_first_of(const wchar_t* sz
, size_t nStart
= 0) const;
1317 size_t find_first_of(const char* sz
, size_t nStart
, size_t n
) const;
1318 size_t find_first_of(const wchar_t* sz
, size_t nStart
, size_t n
) const;
1319 size_t find_first_of(wxUniChar c
, size_t nStart
= 0) const;
1320 size_t find_last_of (const wxString
& str
, size_t nStart
= npos
) const;
1321 size_t find_last_of (const char* sz
, size_t nStart
= npos
) const;
1322 size_t find_last_of (const wchar_t* sz
, size_t nStart
= npos
) const;
1323 size_t find_last_of(const char* sz
, size_t nStart
, size_t n
) const;
1324 size_t find_last_of(const wchar_t* sz
, size_t nStart
, size_t n
) const;
1325 size_t find_last_of(wxUniChar c
, size_t nStart
= npos
) const;
1326 size_t find_first_not_of(const wxString
& str
, size_t nStart
= 0) const;
1327 size_t find_first_not_of(const char* sz
, size_t nStart
= 0) const;
1328 size_t find_first_not_of(const wchar_t* sz
, size_t nStart
= 0) const;
1329 size_t find_first_not_of(const char* sz
, size_t nStart
, size_t n
) const;
1330 size_t find_first_not_of(const wchar_t* sz
, size_t nStart
, size_t n
) const;
1331 size_t find_first_not_of(wxUniChar ch
, size_t nStart
= 0) const;
1332 size_t find_last_not_of(const wxString
& str
, size_t nStart
= npos
) const;
1333 size_t find_last_not_of(const char* sz
, size_t nStart
= npos
) const;
1334 size_t find_last_not_of(const wchar_t* sz
, size_t nStart
= npos
) const;
1335 size_t find_last_not_of(const char* sz
, size_t nStart
, size_t n
) const;
1336 size_t find_last_not_of(const wchar_t* sz
, size_t nStart
, size_t n
) const;
1338 wxString
& insert(size_t nPos
, const wxString
& str
);
1339 wxString
& insert(size_t nPos
, const wxString
& str
, size_t nStart
, size_t n
);
1340 wxString
& insert(size_t nPos
, const char *sz
, size_t n
);
1341 wxString
& insert(size_t nPos
, const wchar_t *sz
, size_t n
);
1342 wxString
& insert(size_t nPos
, size_t n
, wxUniChar ch
);
1343 iterator
insert(iterator it
, wxUniChar ch
);
1344 void insert(iterator it
, const_iterator first
, const_iterator last
);
1345 void insert(iterator it
, size_type n
, wxUniChar ch
);
1347 size_t length() const;
1349 size_type
max_size() const;
1351 void reserve(size_t sz
);
1352 void resize(size_t nSize
, wxUniChar ch
= '\0');
1354 wxString
& replace(size_t nStart
, size_t nLen
, const wxString
& str
);
1355 wxString
& replace(size_t nStart
, size_t nLen
, size_t nCount
, wxUniChar ch
);
1356 wxString
& replace(size_t nStart
, size_t nLen
,
1357 const wxString
& str
, size_t nStart2
, size_t nLen2
);
1358 wxString
& replace(size_t nStart
, size_t nLen
,
1359 const char* sz
, size_t nCount
);
1360 wxString
& replace(size_t nStart
, size_t nLen
,
1361 const wchar_t* sz
, size_t nCount
);
1362 wxString
& replace(size_t nStart
, size_t nLen
,
1363 const wxString
& s
, size_t nCount
);
1364 wxString
& replace(iterator first
, iterator last
, const wxString
& s
);
1365 wxString
& replace(iterator first
, iterator last
, const char* s
, size_type n
);
1366 wxString
& replace(iterator first
, iterator last
, const wchar_t* s
, size_type n
);
1367 wxString
& replace(iterator first
, iterator last
, size_type n
, wxUniChar ch
);
1368 wxString
& replace(iterator first
, iterator last
,
1369 const_iterator first1
, const_iterator last1
);
1370 wxString
& replace(iterator first
, iterator last
,
1371 const char *first1
, const char *last1
);
1372 wxString
& replace(iterator first
, iterator last
,
1373 const wchar_t *first1
, const wchar_t *last1
);
1375 size_t rfind(const wxString
& str
, size_t nStart
= npos
) const;
1376 size_t rfind(const char* sz
, size_t nStart
= npos
, size_t n
= npos
) const;
1377 size_t rfind(const wchar_t* sz
, size_t nStart
= npos
, size_t n
= npos
) const;
1378 size_t rfind(wxUniChar ch
, size_t nStart
= npos
) const;
1380 size_type
size() const;
1381 wxString
substr(size_t nStart
= 0, size_t nLen
= npos
) const;
1382 void swap(wxString
& str
);
1389 // Keep these functions separed from the other groups or Doxygen gets confused
1390 // -----------------------------------------------------------------------------
1393 An 'invalid' value for string index
1395 static const size_t npos
;
1398 This static function returns the string containing the result of calling
1399 Printf() with the passed parameters on it.
1401 @see FormatV(), Printf()
1403 static wxString
Format(const wxString
& format
, ...);
1406 This static function returns the string containing the result of calling
1407 PrintfV() with the passed parameters on it.
1409 @see Format(), PrintfV()
1411 static wxString
FormatV(const wxString
& format
, va_list argptr
);
1415 Converts given buffer of binary data from 8-bit string to wxString. In
1416 Unicode build, the string is interpreted as being in ISO-8859-1
1417 encoding. The version without @e len parameter takes NUL-terminated
1420 This is a convenience method useful when storing binary data in
1421 wxString. It should be used @em only for that purpose and only in
1422 conjunction with To8BitData(). Use mb_str() for conversion of character
1423 data to known encoding.
1427 @see wxString::To8BitData()
1429 static wxString
From8BitData(const char* buf
, size_t len
);
1430 static wxString
From8BitData(const char* buf
);
1435 Converts the string or character from an ASCII, 7-bit form
1436 to the native wxString representation.
1438 static wxString
FromAscii(const char* s
);
1439 static wxString
FromAscii(const unsigned char* s
);
1440 static wxString
FromAscii(const char* s
, size_t len
);
1441 static wxString
FromAscii(const unsigned char* s
, size_t len
);
1442 static wxString
FromAscii(char c
);
1447 Converts C string encoded in UTF-8 to wxString.
1449 If @a s is not a valid UTF-8 string, an empty string is returned.
1451 Notice that when using UTF-8 wxWidgets build there is a more efficient
1452 alternative to this function called FromUTF8Unchecked() which, unlike
1453 this one, doesn't check that the input string is valid.
1457 static wxString
FromUTF8(const char* s
);
1458 static wxString
FromUTF8(const char* s
, size_t len
);
1463 Converts C string encoded in UTF-8 to wxString without checking its
1466 This method assumes that @a s is a valid UTF-8 sequence and doesn't do
1467 any validation (although an assert failure is triggered in debug builds
1468 if the string is invalid). Only use it if you are absolutely sure that
1469 @a s is a correct UTF-8 string (e.g. because it comes from another
1470 library using UTF-8) and if the performance matters, otherwise use
1471 slower (in UTF-8 build) but safer FromUTF8(). Passing a bad UTF-8
1472 string to this function will result in creating a corrupted wxString
1473 and all the subsequent operations on it will be undefined.
1477 static wxString
FromUTF8Unchecked(const char* s
);
1478 static wxString
FromUTF8Unchecked(const char* s
, size_t len
);
1486 Comparison operator for string types.
1488 inline bool operator==(const wxString
& s1
, const wxString
& s2
);
1489 inline bool operator!=(const wxString
& s1
, const wxString
& s2
);
1490 inline bool operator< (const wxString
& s1
, const wxString
& s2
);
1491 inline bool operator> (const wxString
& s1
, const wxString
& s2
);
1492 inline bool operator<=(const wxString
& s1
, const wxString
& s2
);
1493 inline bool operator>=(const wxString
& s1
, const wxString
& s2
);
1494 inline bool operator==(const wxString
& s1
, const wxCStrData
& s2
);
1495 inline bool operator==(const wxCStrData
& s1
, const wxString
& s2
);
1496 inline bool operator!=(const wxString
& s1
, const wxCStrData
& s2
);
1497 inline bool operator!=(const wxCStrData
& s1
, const wxString
& s2
);
1498 inline bool operator==(const wxString
& s1
, const wxWCharBuffer
& s2
);
1499 inline bool operator==(const wxWCharBuffer
& s1
, const wxString
& s2
);
1500 inline bool operator!=(const wxString
& s1
, const wxWCharBuffer
& s2
);
1501 inline bool operator!=(const wxWCharBuffer
& s1
, const wxString
& s2
);
1502 inline bool operator==(const wxString
& s1
, const wxCharBuffer
& s2
);
1503 inline bool operator==(const wxCharBuffer
& s1
, const wxString
& s2
);
1504 inline bool operator!=(const wxString
& s1
, const wxCharBuffer
& s2
);
1505 inline bool operator!=(const wxCharBuffer
& s1
, const wxString
& s2
);
1510 Comparison operators char types.
1512 inline bool operator==(const wxUniChar
& c
, const wxString
& s
);
1513 inline bool operator==(const wxUniCharRef
& c
, const wxString
& s
);
1514 inline bool operator==(char c
, const wxString
& s
);
1515 inline bool operator==(wchar_t c
, const wxString
& s
);
1516 inline bool operator==(int c
, const wxString
& s
);
1517 inline bool operator==(const wxString
& s
, const wxUniChar
& c
);
1518 inline bool operator==(const wxString
& s
, const wxUniCharRef
& c
);
1519 inline bool operator==(const wxString
& s
, char c
);
1520 inline bool operator==(const wxString
& s
, wchar_t c
);
1521 inline bool operator!=(const wxUniChar
& c
, const wxString
& s
);
1522 inline bool operator!=(const wxUniCharRef
& c
, const wxString
& s
);
1523 inline bool operator!=(char c
, const wxString
& s
);
1524 inline bool operator!=(wchar_t c
, const wxString
& s
);
1525 inline bool operator!=(int c
, const wxString
& s
);
1526 inline bool operator!=(const wxString
& s
, const wxUniChar
& c
);
1527 inline bool operator!=(const wxString
& s
, const wxUniCharRef
& c
);
1528 inline bool operator!=(const wxString
& s
, char c
);
1529 inline bool operator!=(const wxString
& s
, wchar_t c
);
1533 The global wxString instance of an empty string.
1534 Used extensively in the entire wxWidgets API.
1536 wxString wxEmptyString
;
1541 @class wxStringBufferLength
1543 This tiny class allows you to conveniently access the wxString internal buffer
1544 as a writable pointer without any risk of forgetting to restore the string to
1545 the usable state later, and allows the user to set the internal length of the string.
1547 For example, assuming you have a low-level OS function called
1548 @c "int GetMeaningOfLifeAsString(char *)" copying the value in the provided
1549 buffer (which must be writable, of course), and returning the actual length
1550 of the string, you might call it like this:
1554 wxStringBufferLength theAnswerBuffer(theAnswer, 1024);
1555 int nLength = GetMeaningOfLifeAsString(theAnswerBuffer);
1556 theAnswerBuffer.SetLength(nLength);
1557 if ( theAnswer != "42" )
1558 wxLogError("Something is very wrong!");
1561 Note that the exact usage of this depends on whether or not wxUSE_STL is
1562 enabled. If wxUSE_STL is enabled, wxStringBuffer creates a separate empty
1563 character buffer, and if wxUSE_STL is disabled, it uses GetWriteBuf() from
1564 wxString, keeping the same buffer wxString uses intact. In other words,
1565 relying on wxStringBuffer containing the old wxString data is not a good
1566 idea if you want to build your program both with and without wxUSE_STL.
1568 Note that wxStringBuffer::SetLength @b must be called before
1569 wxStringBufferLength destructs.
1574 class wxStringBufferLength
1578 Constructs a writable string buffer object associated with the given string
1579 and containing enough space for at least @a len characters.
1581 Basically, this is equivalent to calling wxString::GetWriteBuf and
1584 wxStringBufferLength(const wxString
& str
, size_t len
);
1587 Restores the string passed to the constructor to the usable state by calling
1588 wxString::UngetWriteBuf on it.
1590 ~wxStringBufferLength();
1593 Sets the internal length of the string referred to by wxStringBufferLength to
1594 @a nLength characters.
1596 Must be called before wxStringBufferLength destructs.
1598 void SetLength(size_t nLength
);
1601 Returns the writable pointer to a buffer of the size at least equal to the
1602 length specified in the constructor.
1604 wxChar
* operator wxChar
*();
1609 @class wxStringBuffer
1611 This tiny class allows you to conveniently access the wxString internal buffer
1612 as a writable pointer without any risk of forgetting to restore the string
1613 to the usable state later.
1615 For example, assuming you have a low-level OS function called
1616 @c "GetMeaningOfLifeAsString(char *)" returning the value in the provided
1617 buffer (which must be writable, of course) you might call it like this:
1621 GetMeaningOfLifeAsString(wxStringBuffer(theAnswer, 1024));
1622 if ( theAnswer != "42" )
1623 wxLogError("Something is very wrong!");
1626 Note that the exact usage of this depends on whether or not @c wxUSE_STL is
1627 enabled. If @c wxUSE_STL is enabled, wxStringBuffer creates a separate empty
1628 character buffer, and if @c wxUSE_STL is disabled, it uses GetWriteBuf() from
1629 wxString, keeping the same buffer wxString uses intact. In other words,
1630 relying on wxStringBuffer containing the old wxString data is not a good
1631 idea if you want to build your program both with and without @c wxUSE_STL.
1636 class wxStringBuffer
1640 Constructs a writable string buffer object associated with the given string
1641 and containing enough space for at least @a len characters.
1642 Basically, this is equivalent to calling wxString::GetWriteBuf() and
1645 wxStringBuffer(const wxString
& str
, size_t len
);
1648 Restores the string passed to the constructor to the usable state by calling
1649 wxString::UngetWriteBuf() on it.
1654 Returns the writable pointer to a buffer of the size at least equal to the
1655 length specified in the constructor.
1657 wxStringCharType
* operator wxStringCharType
*();
1661 /** @addtogroup group_funcmacro_string */
1665 Allows to extend a function with the signature:
1666 @code bool SomeFunc(const wxUniChar& c) @endcode
1667 which operates on a single character, to an entire wxString.
1669 E.g. if you want to check if an entire string contains only digits,
1672 if (wxStringCheck<wxIsdigit>(myString))
1673 ... // the entire string contains oly digits!
1675 ... // at least one character of myString is not a digit
1678 @return @true if the given function returns a non-zero value for all
1679 characters of the @a val string.
1681 template<bool (T
)(const wxUniChar
& c
)>
1682 inline bool wxStringCheck(const wxString
& val
);