1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: interface of wxStringBuffer
4 // Author: wxWidgets team
6 // Licence: wxWindows license
7 /////////////////////////////////////////////////////////////////////////////
12 This tiny class allows you to conveniently access the wxString
13 internal buffer as a writable pointer without any risk of forgetting to restore
14 the string to the usable state later.
16 For example, assuming you have a low-level OS function called
17 @c GetMeaningOfLifeAsString(char *) returning the value in the provided
18 buffer (which must be writable, of course) you might call it like this:
22 GetMeaningOfLifeAsString(wxStringBuffer(theAnswer, 1024));
23 if ( theAnswer != "42" )
25 wxLogError("Something is very wrong!");
29 Note that the exact usage of this depends on whether or not wxUSE_STL is
30 enabled. If wxUSE_STL is enabled, wxStringBuffer creates a separate empty
31 character buffer, and if wxUSE_STL is disabled, it uses GetWriteBuf() from
32 wxString, keeping the same buffer wxString uses intact. In other words,
33 relying on wxStringBuffer containing the old wxString data is not a good
34 idea if you want to build your program both with and without wxUSE_STL.
43 Constructs a writable string buffer object associated with the given string
44 and containing enough space for at least @a len characters. Basically, this
45 is equivalent to calling wxString::GetWriteBuf and
48 wxStringBuffer(const wxString
& str
, size_t len
);
51 Restores the string passed to the constructor to the usable state by calling
52 wxString::UngetWriteBuf on it.
57 Returns the writable pointer to a buffer of the size at least equal to the
58 length specified in the constructor.
60 wxStringCharType
* operator wxStringCharType
*();
68 wxString is a class representing a Unicode character string.
69 wxString uses @c std::string internally to store its content
70 unless this is not supported by the compiler or disabled
71 specifically when building wxWidgets. Therefore wxString
72 inherits many features from @c std::string. Most
73 implementations of @c std::string are thread-safe and don't
74 use reference counting. By default, wxString uses @c std::string
75 internally even if wxUSE_STL is not defined.
77 Since wxWidgets 3.0 wxString internally uses UCS-2 (basically 2-byte per
78 character wchar_t and nearly the same as UTF-16) under Windows and
79 UTF-8 under Unix, Linux and OS X to store its content.
80 Much work has been done to make existing code using ANSI string literals
81 work as before. If you need to have a wxString that uses wchar_t on Unix
82 and Linux, too, you can specify this on the command line with the
83 @c configure @c --disable-utf8 switch.
85 If you need a Unicode string class with O(1) access on all platforms
86 you should consider using wxUString.
88 Since iterating over a wxString by index can become inefficient in UTF-8
89 mode iterators should be used instead of index based access:
93 wxString::const_iterator i;
94 for (i = s.begin(); i != s.end(); ++i)
96 wxUniChar uni_ch = *i;
97 // do something with it
102 @ref overview_string "wxString overview" and the
103 @ref overview_unicode "Unicode overview" for more information
106 wxString uses the current locale encoding to convert any C string
107 literal to Unicode. The same is done for converting to and from
108 @c std::string and for the return value of c_str(). For this
109 conversion, the @a wxConvLibc class instance is used. See wxCSConv and wxMBConv.
111 wxString implements most of the methods of the @c std::string class.
112 These standard functions are only listed here, but they are not
113 fully documented in this manual. Please see the STL documentation.
114 The behaviour of all these functions is identical to the behaviour
117 You may notice that wxString sometimes has several functions which do
118 the same thing like Length(), Len() and length() which
119 all return the string length. In all cases of such duplication the
120 @c std::string compatible method should be used.
122 Anything may be concatenated (appended to) with a string. However, you can't
123 append something to a C string (including literal constants), so to do this it
124 should be converted to a wxString first.
134 A string may be constructed either from a C string, (some number of copies of)
135 a single character or a wide (Unicode) string. For all constructors (except the
136 default which creates an empty string) there is also a corresponding assignment
144 The MakeXXX() variants modify the string in place, while the other functions
145 return a new string which contains the original text converted to the upper or
146 lower case and leave the original string unchanged.
152 @li MakeCapitalized()
155 Many functions below take a character index in the string. As with C
156 strings and arrays, the indices start from 0, so the first character of a
157 string is string[0]. An attempt to access a character beyond the end of the
158 string (which may even be 0 if the string is empty) will provoke an assert
159 failure in @ref overview_debugging "debug build", but no checks are
160 done in release builds.
161 This section also contains both implicit and explicit conversions to C style
162 strings. Although implicit conversion is quite convenient, you are advised
163 to use wc_str() for the sake of clarity.
166 @li GetWritableChar()
177 The default comparison function Cmp() is case-sensitive and so is the default
178 version of IsSameAs(). For case insensitive comparisons you should use CmpNoCase()
179 or give a second parameter to IsSameAs(). This last function is maybe more
180 convenient if only equality of the strings matters because it returns a boolean
181 @true value if the strings are the same and not 0 (which is usually @false
183 Matches() is a poor man's regular expression matcher: it only understands
184 '*' and '?' metacharacters in the sense of DOS command line interpreter.
185 StartsWith() is helpful when parsing a line of text which should start
186 with some predefined prefix and is more efficient than doing direct string
187 comparison as you would also have to precalculate the length of the prefix.
197 The string provides functions for conversion to signed and unsigned integer and
198 floating point numbers. All functions take a pointer to the variable to
199 put the numeric value in and return @true if the @b entire string could be
200 converted to a number.
208 The following are "advanced" functions and they will be needed rarely.
209 Alloc() and Shrink() are only interesting for optimization purposes.
210 wxStringBuffer and wxStringBufferLength classes may be very useful
211 when working with some external API which requires the caller to provide
219 @li wxStringBufferLength
221 Miscellaneous other string functions.
227 These functions return the string length and check whether the string
228 is empty or they empty it.
238 These functions allow you to extract a substring from the string. The
239 original string is not modified and the function returns the extracted
254 These functions replace the standard @e strchr() and @e strstr()
263 Both formatted versions (Printf/() and stream-like insertion operators
264 exist (for basic types only). Additionally, the Format() function allows
265 you to simply append a formatted value to a string:
273 The following functions are deprecated. Please consider using new wxWidgets 2.0
274 functions instead (or, even better, @c std::string compatible variants).
276 Contains(), First(), Freq(), IsAscii(), IsNull(),
277 IsNumber(), IsWord(), Last(), Length(), LowerCase(), Remove(), Strip(),
278 SubString(), UpperCase()
284 ::Objects, ::wxEmptyString,
286 @see @ref overview_string "wxString overview", @ref overview_unicode
287 "Unicode overview", wxUString
293 An 'invalid' value for string index
295 static const size_t npos
;
301 typedef wxUniChar value_type
;
302 typedef wxUniChar char_type
;
303 typedef wxUniCharRef reference
;
304 typedef wxChar
* pointer
;
305 typedef const wxChar
* const_pointer
;
306 typedef size_t size_type
;
307 typedef wxUniChar const_reference
;
316 Creates a string from another string. Just increases the ref
319 wxString(const wxString
& stringSrc
);
323 Constructs a string from the string literal @e psz using
324 the current locale encoding to convert it to Unicode (wxConvLibc).
326 wxString(const char *psz
);
329 Constructs a string from the string literal @e psz using
330 @e conv to convert it Unicode.
332 wxString(const char *psz
, const wxMBConv
& conv
);
335 Constructs a string from the first @e nLength character of the string literal @e psz using
336 the current locale encoding to convert it to Unicode (wxConvLibc).
338 wxString(const char *psz
, size_t nLength
);
341 Constructs a string from the first @e nLength character of the string literal @e psz using
342 @e conv to convert it Unicode.
344 wxString(const char *psz
, const wxMBConv
& conv
, size_t nLength
);
347 Constructs a string from the string literal @e pwz.
349 wxString(const wchar_t *pwz
);
352 Constructs a string from the first @e nLength characters of the string literal @e pwz.
354 wxString(const wchar_t *pwz
, size_t nLength
);
357 Constructs a string from @e buf using the using
358 the current locale encoding to convert it to Unicode.
360 wxString(const wxCharBuffer
& buf
);
363 Constructs a string from @e buf.
365 wxString(const wxWCharBuffer
& buf
);
368 Constructs a string from @e str using the using the current locale encoding
369 to convert it to Unicode (wxConvLibc).
371 wxString(const std::string
& str
);
374 Constructs a string from @e str.
376 wxString(const std::wstring
& str
);
380 String destructor. Note that this is not virtual, so wxString must not be
386 Gets all the characters after the first occurrence of @e ch.
387 Returns the empty string if @e ch is not found.
389 wxString
AfterFirst(wxUniChar ch
) const;
392 Gets all the characters after the last occurrence of @e ch.
393 Returns the whole string if @e ch is not found.
395 wxString
AfterLast(wxUniChar ch
) const;
398 Preallocate enough space for wxString to store @a nLen characters.
400 Please note that this method does the same thing as the standard
401 reserve() one and shouldn't be used in new code.
403 This function may be used to increase speed when the string is
404 constructed by repeated concatenation as in
407 // delete all vowels from the string
408 wxString DeleteAllVowels(const wxString& original)
412 size_t len = original.length();
416 for ( size_t n = 0; n < len; n++ )
418 if ( strchr("aeuio", tolower(original[n])) == NULL )
419 result += original[n];
426 because it will avoid the need to reallocate string memory many times
427 (in case of long strings). Note that it does not set the maximal length
428 of a string -- it will still expand if more than @a nLen characters are
429 stored in it. Also, it does not truncate the existing string (use
430 Truncate() for this) even if its current length is greater than @a nLen.
432 @return @true if memory was successfully allocated, @false otherwise.
434 bool Alloc(size_t nLen
);
437 Appends the string literal @e psz.
439 wxString
& Append(const char* psz
);
442 Appends the wide string literal @e pwz.
444 wxString
& Append(const wchar_t* pwz
)
447 Appends the string literal @e psz with max length @e nLen.
449 wxString
& Append(const char* psz
, size_t nLen
);
452 Appends the wide string literal @e psz with max length @e nLen.
454 wxString
& Append(const wchar_t* pwz
, size_t nLen
)
457 Appends the string @e s.
459 wxString
&Append(const wxString
&s
);
462 Appends the character @e ch @e count times.
464 wxString
&Append(wxUniChar ch
, size_t count
= 1u);
467 Gets all characters before the first occurrence of @e ch.
468 Returns the whole string if @a ch is not found.
470 wxString
BeforeFirst(wxUniChar ch
) const;
473 Gets all characters before the last occurrence of @e ch.
474 Returns the empty string if @a ch is not found.
476 wxString
BeforeLast(wxUniChar ch
) const;
480 Return the copy of the string with the first string character in the
481 upper case and the subsequent ones in the lower case.
485 @see MakeCapitalized()
487 wxString
Capitalize() const;
490 Empties the string and frees memory occupied by it.
496 Returns a deep copy of the string.
498 That is, the returned string is guaranteed to not share data with this
499 string when using reference-counted wxString implementation.
501 This method is primarily useful for passing strings between threads
502 (because wxString is not thread-safe). Unlike creating a copy using
503 @c wxString(c_str()), Clone() handles embedded NULs correctly.
507 wxString
Clone() const;
510 Case-sensitive comparison.
511 Returns a positive value if the string is greater than the argument,
512 zero if it is equal to it or a negative value if it is less than the
513 argument (same semantics as the standard @c strcmp() function).
515 See also CmpNoCase(), IsSameAs().
517 int Cmp(const wxString
& s
) const;
520 Case-insensitive comparison.
521 Returns a positive value if the string is greater than the argument,
522 zero if it is equal to it or a negative value if it is less than the
523 argument (same semantics as the standard @c strcmp() function).
525 See also Cmp(), IsSameAs().
527 int CmpNoCase(const wxString
& s
) const;
534 bool operator ==(const wxString
& x
, const wxString
& y
);
535 bool operator ==(const wxString
& x
, wxUniChar ch
);
536 bool operator !=(const wxString
& x
, const wxString
& y
);
537 bool operator !=(const wxString
& x
, wxUniChar ch
);
538 bool operator(const wxString
& x
, const wxString
& y
);
539 bool operator(const wxString
& x
, wxUniChar ch
);
540 bool operator =(const wxString
& x
, const wxString
& y
);
541 bool operator =(const wxString
& x
, wxUniChar ch
);
542 bool operator(const wxString
& x
, const wxString
& y
);
543 bool operator(const wxString
& x
, wxUniChar ch
);
544 bool operator =(const wxString
& x
, const wxString
& y
);
545 bool operator =(const wxString
& x
, wxUniChar ch
);
550 Returns @true if target appears anywhere in wxString; else @false.
551 This is a wxWidgets 1.xx compatibility function; you should not use it in new
554 bool Contains(const wxString
& str
) const;
558 Makes the string empty, but doesn't free memory occupied by the string.
564 This function can be used to test if the string ends with the specified
565 @e suffix. If it does, the function will return @true and put the
566 beginning of the string before the suffix into @e rest string if it is not
567 @NULL. Otherwise, the function returns @false and doesn't
570 bool EndsWith(const wxString
& suffix
, wxString
*rest
= NULL
) const;
573 Searches for the given character @e ch. Returns the position or
574 @c wxNOT_FOUND if not found.
576 int Find(wxUniChar ch
, bool fromEnd
= false) const;
579 Searches for the given string @e sub. Returns the starting position or
580 @c wxNOT_FOUND if not found.
582 int Find(const wxString
& sub
) const;
587 This is a wxWidgets 1.xx compatibility function;
588 you should not use it in new code.
590 int First(wxUniChar ch
) const;
591 int First(const wxString
& str
) const;
595 This static function returns the string containing the result of calling
596 Printf() with the passed parameters on it.
598 @see FormatV(), Printf()
600 static wxString
Format(const wxChar format
, ...);
603 This static function returns the string containing the result of calling
604 PrintfV() with the passed parameters on it.
606 @see Format(), PrintfV()
608 static wxString
FormatV(const wxChar format
, va_list argptr
);
611 Returns the number of occurrences of @e ch in the string.
612 This is a wxWidgets 1.xx compatibility function; you should not
615 int Freq(wxUniChar ch
) const;
619 Converts given buffer of binary data from 8-bit string to wxString. In
620 Unicode build, the string is interpreted as being in ISO-8859-1
621 encoding. The version without @e len parameter takes NUL-terminated
624 This is a convenience method useful when storing binary data in
625 wxString. It should be used @em only for that purpose and only in
626 conjunction with To8BitData(). Use mb_str() for conversion of character
627 data to known encoding.
631 @see wxString::To8BitData()
633 static wxString
From8BitData(const char* buf
, size_t len
);
634 static wxString
From8BitData(const char* buf
);
639 Converts the string or character from an ASCII, 7-bit form
640 to the native wxString representation.
642 static wxString
FromAscii(const char* s
);
643 static wxString
FromAscii(const unsigned char* s
);
644 static wxString
FromAscii(const char* s
, size_t len
);
645 static wxString
FromAscii(const unsigned char* s
, size_t len
);
646 static wxString
FromAscii(char c
);
651 Converts C string encoded in UTF-8 to wxString.
653 If @a s is not a valid UTF-8 string, an empty string is returned.
655 Notice that when using UTF-8 wxWidgets build there is a more efficient
656 alternative to this function called FromUTF8Unchecked() which, unlike
657 this one, doesn't check that the input string is valid.
661 static wxString
FromUTF8(const char* s
);
662 static wxString
FromUTF8(const char* s
, size_t len
);
667 Converts C string encoded in UTF-8 to wxString without checking its
670 This method assumes that @a s is a valid UTF-8 sequence and doesn't do
671 any validation (although an assert failure is triggered in debug builds
672 if the string is invalid). Only use it if you are absolutely sure that
673 @a s is a correct UTF-8 string (e.g. because it comes from another
674 library using UTF-8) and if the performance matters, otherwise use
675 slower (in UTF-8 build) but safer FromUTF8(). Passing a bad UTF-8
676 string to this function will result in creating a corrupted wxString
677 and all the subsequent operations on it will be undefined.
681 static wxString
FromUTF8Unchecked(const char* s
);
682 static wxString
FromUTF8Unchecked(const char* s
, size_t len
);
686 Returns the character at position @a n (read-only).
688 wxUniChar
GetChar(size_t n
) const;
691 wxWidgets compatibility conversion. Same as c_str().
693 const wxCStrData
* GetData() const;
696 Returns a reference to the character at position @e n.
698 wxUniCharRef
GetWritableChar(size_t n
);
701 Returns a writable buffer of at least @a len bytes.
702 It returns a pointer to a new memory block, and the
703 existing data will not be copied.
704 Call UngetWriteBuf() as soon as possible to put the
705 string back into a reasonable state.
706 This method is deprecated, please use wxStringBuffer or
707 wxStringBufferLength instead.
709 wxStringCharType
* GetWriteBuf(size_t len
);
712 Returns @true if the string contains only ASCII characters.
713 This is a wxWidgets 1.xx compatibility function; you should not use it in new
716 bool IsAscii() const;
719 Returns @true if the string is empty.
721 bool IsEmpty() const;
724 Returns @true if the string is empty (same as wxString::IsEmpty).
725 This is a wxWidgets 1.xx compatibility function; you should not use it in new
731 Returns @true if the string is an integer (with possible sign).
732 This is a wxWidgets 1.xx compatibility function; you should not use it in new
735 bool IsNumber() const;
739 Test whether the string is equal to the single character @e c. The test is
740 case-sensitive if @a caseSensitive is @true (default) or not if it is @c
742 Returns @true if the string is equal to the character, @false otherwise.
743 See also Cmp(), CmpNoCase()
745 bool IsSameAs(const wxString
&s
, bool caseSensitive
= true) const;
746 bool IsSameAs(wxUniChar ch
, bool caseSensitive
= true) const;
750 Returns @true if the string is a word.
751 This is a wxWidgets 1.xx compatibility function; you should not use it in new
758 Returns a reference to the last character (writable).
759 This is a wxWidgets 1.xx compatibility function;
760 you should not use it in new code.
763 const wxUniChar
Last();
767 Returns the first @a count characters of the string.
769 wxString
Left(size_t count
) const;
772 Returns the length of the string.
777 Returns the length of the string (same as Len).
778 This is a wxWidgets 1.xx compatibility function; you should not use it in new
781 size_t Length() const;
784 Returns this string converted to the lower case.
788 wxString
Lower() const;
792 This is a wxWidgets 1.xx compatibility function; you should not use it in new
798 Converts the first characters of the string to the upper case and all
799 the subsequent ones to the lower case and returns the result.
805 wxString
& MakeCapitalized();
808 Converts all characters to lower case and returns the result.
812 wxString
& MakeLower();
815 Converts all characters to upper case and returns the result.
819 wxString
& MakeUpper();
822 Returns @true if the string contents matches a mask containing '*' and '?'.
824 bool Matches(const wxString
& mask
) const;
827 Returns a substring starting at @e first, with length @e count, or the rest of
828 the string if @a count is the default value.
830 wxString
Mid(size_t first
, size_t count
= wxSTRING_MAXLEN
) const;
834 Adds @a count copies of @a pad to the beginning, or to the end of the
835 string (the default). Removes spaces from the left or from the right (default).
837 wxString
& Pad(size_t count
, wxUniChar pad
= ' ',
838 bool fromRight
= true);
841 Prepends @a str to this string, returning a reference to this string.
843 wxString
& Prepend(const wxString
& str
);
846 Similar to the standard function @e sprintf(). Returns the number of
847 characters written, or an integer less than zero on error.
848 Note that if @c wxUSE_PRINTF_POS_PARAMS is set to 1, then this function supports
849 Unix98-style positional parameters:
851 @note This function will use a safe version of @e vsprintf() (usually called
852 @e vsnprintf()) whenever available to always allocate the buffer of correct
853 size. Unfortunately, this function is not available on all platforms and the
854 dangerous @e vsprintf() will be used then which may lead to buffer overflows.
856 int Printf(const wxChar
* pszFormat
, ...);
859 Similar to vprintf. Returns the number of characters written, or an integer
863 int PrintfV(const wxChar
* pszFormat
, va_list argPtr
);
867 Removes @a len characters from the string, starting at @e pos.
868 This is a wxWidgets 1.xx compatibility function; you should not use it in new
871 wxString
Remove(size_t pos
);
872 wxString
Remove(size_t pos
, size_t len
);
876 Removes the last character.
878 wxString
RemoveLast();
881 Replace first (or all) occurrences of substring with another one.
882 @e replaceAll: global replace (default), or only the first occurrence.
883 Returns the number of replacements made.
885 size_t Replace(const wxString
& strOld
, const wxString
& strNew
,
886 bool replaceAll
= true);
889 Returns the last @a count characters.
891 wxString
Right(size_t count
) const;
894 Sets the character at position @e n.
896 void SetChar(size_t n
, wxUniChar ch
);
899 Minimizes the string's memory. This can be useful after a call to
900 Alloc() if too much memory were preallocated.
905 This function can be used to test if the string starts with the specified
906 @e prefix. If it does, the function will return @true and put the rest
907 of the string (i.e. after the prefix) into @a rest string if it is not
908 @NULL. Otherwise, the function returns @false and doesn't modify the
911 bool StartsWith(const wxString
& prefix
, wxString
*rest
= NULL
) const;
914 Strip characters at the front and/or end. The same as Trim except that it
915 doesn't change this string.
916 This is a wxWidgets 1.xx compatibility function; you should not use it in new
919 wxString
Strip(stripType s
= trailing
) const;
922 Returns the part of the string between the indices @a from and @e to
924 This is a wxWidgets 1.xx compatibility function, use Mid()
925 instead (but note that parameters have different meaning).
927 wxString
SubString(size_t from
, size_t to
) const;
931 Converts the string to an 8-bit string in ISO-8859-1 encoding in the
932 form of a wxCharBuffer (Unicode builds only).
934 This is a convenience method useful when storing binary data in
935 wxString. It should be used @em only for this purpose. It is only valid
936 to call this method on strings created using From8BitData().
940 @see wxString::From8BitData()
942 const char* To8BitData() const;
943 const wxCharBuffer
To8BitData() const;
948 Converts the string to an ASCII, 7-bit string in the form of
949 a wxCharBuffer (Unicode builds only) or a C string (ANSI builds).
950 Note that this conversion only works if the string contains only ASCII
951 characters. The @ref mb_str() "mb_str" method provides more
952 powerful means of converting wxString to C string.
954 const char* ToAscii() const;
955 const wxCharBuffer
ToAscii() const;
959 Attempts to convert the string to a floating point number. Returns @true on
960 success (the number is stored in the location pointed to by @e val) or @false
961 if the string does not represent such number (the value of @a val is not
962 modified in this case).
964 @see ToLong(), ToULong()
966 bool ToDouble(double val
) const;
969 Attempts to convert the string to a signed integer in base @e base. Returns
970 @true on success in which case the number is stored in the location
971 pointed to by @a val or @false if the string does not represent a
972 valid number in the given base (the value of @a val is not modified
974 The value of @a base must be comprised between 2 and 36, inclusive, or
975 be a special value 0 which means that the usual rules of @c C numbers are
976 applied: if the number starts with @c 0x it is considered to be in base
977 16, if it starts with @c 0 - in base 8 and in base 10 otherwise. Note
978 that you may not want to specify the base 0 if you are parsing the numbers
979 which may have leading zeroes as they can yield unexpected (to the user not
980 familiar with C) results.
982 @see ToDouble(), ToULong()
984 bool ToLong(long val
, int base
= 10) const;
987 This is exactly the same as ToLong() but works with 64
989 Notice that currently it doesn't work (always returns @false) if parsing of 64
990 bit numbers is not supported by the underlying C run-time library. Compilers
991 with C99 support and Microsoft Visual C++ version 7 and higher do support this.
993 @see ToLong(), ToULongLong()
995 bool ToLongLong(wxLongLong_t val
, int base
= 10) const;
998 Attempts to convert the string to an unsigned integer in base @e base.
999 Returns @true on success in which case the number is stored in the
1000 location pointed to by @a val or @false if the string does not
1001 represent a valid number in the given base (the value of @a val is not
1002 modified in this case). Please notice that this function
1003 behaves in the same way as the standard @c strtoul() and so it simply
1004 converts negative numbers to unsigned representation instead of rejecting them
1005 (e.g. -1 is returned as @c ULONG_MAX).
1006 See ToLong() for the more detailed
1007 description of the @a base parameter.
1009 @see ToDouble(), ToLong()
1011 bool ToULong(unsigned long val
, int base
= 10) const;
1014 This is exactly the same as ToULong() but works with 64
1015 bit integer numbers.
1016 Please see ToLongLong() for additional remarks.
1018 bool ToULongLong(wxULongLong_t val
, int base
= 10) const;
1024 const char* ToUTF8() const;
1025 const wxCharBuffer
ToUTF8() const;
1029 Removes white-space (space, tabs, form feed, newline and carriage return) from
1030 the left or from the right end of the string (right is default).
1032 wxString
& Trim(bool fromRight
= true);
1035 Truncate the string to the given length.
1037 wxString
& Truncate(size_t len
);
1041 Puts the string back into a reasonable state (in which it can be used
1043 GetWriteBuf() was called.
1044 The version of the function without the @a len parameter will calculate the
1045 new string length itself assuming that the string is terminated by the first
1046 @c NUL character in it while the second one will use the specified length
1047 and thus is the only version which should be used with the strings with
1048 embedded @c NULs (it is also slightly more efficient as @c strlen()
1049 doesn't have to be called).
1050 This method is deprecated, please use
1052 wxStringBufferLength instead.
1054 void UngetWriteBuf();
1055 void UngetWriteBuf(size_t len
);
1059 Returns this string converted to upper case.
1063 wxString
Upper() const;
1066 The same as MakeUpper.
1067 This is a wxWidgets 1.xx compatibility function; you should not use it in new
1073 Returns a lightweight intermediate class which is in turn implicitly
1074 convertible to both @c const @c char* and to @c const @c wchar_t*.
1075 Given this ambiguity it is mostly better to use wc_str(), mb_str() or
1078 Please see the @ref overview_unicode "Unicode overview" for more
1079 information about it.
1081 Note that the returned value is not convertible to @c char* or
1082 @c wchar_t*, use char_str() or wchar_str() if you need to pass
1083 string value to a function expecting non-const pointer.
1085 @see wc_str(), utf8_str(), c_str(), mb_str(), fn_str()
1087 const wxCStrData
c_str() const;
1090 Returns an object with string data that is implicitly convertible to
1091 @c char* pointer. Note that any change to the returned buffer is lost and so
1092 this function is only usable for passing strings to legacy libraries that
1093 don't have const-correct API. Use wxStringBuffer if you want to modify
1098 wxWritableCharBuffer
char_str(const wxMBConv
& conv
= wxConvLibc
) const;
1101 Returns buffer of the specified type containing the string data.
1103 This method is only useful in template code, otherwise you should
1104 directly call mb_str() or wc_str() if you need to retrieve a narrow or
1105 wide string from this wxString. The template parameter @a t should be
1106 either @c char or @c wchar_t.
1108 Notice that retrieving a char buffer in UTF-8 build will return the
1109 internal string representation in UTF-8 while in wchar_t build the char
1110 buffer will contain the conversion of the string to the encoding of the
1111 current locale (and so can fail).
1113 @param len If non-@NULL, filled with the length of the returned buffer.
1115 buffer containing the string contents in the specified type,
1116 notice that it may be @NULL if the conversion failed (e.g. Unicode
1117 string couldn't be converted to the current encoding when @a T is
1120 template <typename T
>
1121 wxCharTypeBuffer
<T
> tchar_str(size_t *len
= NULL
) const;
1125 Returns string representation suitable for passing to OS' functions
1128 const wchar_t* fn_str() const;
1129 const char* fn_str() const;
1130 const wxCharBuffer
fn_str() const;
1134 Returns the multibyte (C string) representation of the string
1135 using @e conv's wxMBConv::cWC2MB method and returns wxCharBuffer.
1137 @see wc_str(), utf8_str(), c_str(), wxMBConv
1139 const wxCharBuffer
mb_str(const wxMBConv
& conv
= wxConvLibc
) const;
1142 Extraction from a stream.
1144 friend istream
operator>>(istream
& is
, wxString
& str
);
1147 These functions work as C++ stream insertion operators. They insert the
1148 given value into the string. Precision and format cannot be set using them.
1149 Use Printf() instead.
1151 wxString
& operator<<(const wxString
& s
);
1152 wxString
& operator<<(const char* psz
)
1153 wxString
& operator<<(const wchar_t* pwz
)
1154 wxString
& operator<<(const wxCStrData
& psz
)
1155 wxString
& operator<<(wxUniChar ch
);
1156 wxString
& operator<<(wxUniCharRef ch
)
1157 wxString
& operator<<(char ch
)
1158 wxString
& operator<<(unsigned char ch
)
1159 wxString
& operator<<(wchar_t ch
)
1160 wxString
& operator<<(const wxCharBuffer
& s
)
1161 wxString
& operator<<(const wxWCharBuffer
& s
)
1162 wxString
& operator<<(int i
);
1163 wxString
& operator<<(unsigned int ui
);
1164 wxString
& operator<<(long l
);
1165 wxString
& operator<<(unsigned long ul
);
1166 wxString
& operator<<(wxLongLong_t ll
);
1167 wxString
& operator<<(wxULongLong_t ul
);
1168 wxString
& operator<<(float f
);
1169 wxString
& operator<<(double d
);
1172 Same as Mid (substring extraction).
1174 wxString
operator ()(size_t start
, size_t len
);
1178 Concatenation: these operators return a new string equal to the
1179 concatenation of the operands.
1181 wxString
operator +(const wxString
& x
, const wxString
& y
);
1182 wxString
operator +(const wxString
& x
, wxUniChar y
);
1187 Concatenation in place: the argument is appended to the string.
1189 void operator +=(const wxString
& str
);
1190 void operator +=(wxUniChar c
);
1195 Assignment: the effect of each operation is the same as for the corresponding
1196 constructor (see @ref wxString() "wxString constructors").
1198 wxString
operator =(const wxString
& str
);
1199 wxString
operator =(wxUniChar c
);
1206 wxUniChar
operator [](size_t i
) const;
1207 wxUniCharRef
operator [](size_t i
);
1211 Empty string is @false, so !string will only return @true if the
1216 bool operator!() const;
1221 Converts the strings contents to UTF-8 and returns it either as a
1222 temporary wxCharBuffer object or as a pointer to the internal
1223 string contents in UTF-8 build.
1225 @see wc_str(), c_str(), mb_str()
1227 const char* utf8_str() const;
1228 const wxCharBuffer
utf8_str() const;
1233 Converts the strings contents to the wide character represention
1234 and returns it as a temporary wxWCharBuffer object (Unix and OS X)
1235 or returns a pointer to the internal string contents in wide character
1238 The macro wxWX2WCbuf is defined as the correct return
1239 type (without const).
1241 @see utf8_str(), c_str(), mb_str(), fn_str(), wchar_str()
1243 const wchar_t* wc_str() const;
1244 const wxWCharBuffer
wc_str() const;
1248 Returns an object with string data that is implicitly convertible to
1249 @c char* pointer. Note that changes to the returned buffer may or may
1250 not be lost (depending on the build) and so this function is only usable for
1251 passing strings to legacy libraries that don't have const-correct API. Use
1252 wxStringBuffer if you want to modify the string.
1254 @see mb_str(), wc_str(), fn_str(), c_str(), char_str()
1256 wxWritableWCharBuffer
wchar_str() const;
1259 Explicit conversion to C string in the internal representation (either
1260 wchar_t* or UTF-8-encoded char*, depending on the build).
1262 const wxStringCharType
*wx_str() const;
1266 @name Iterator interface
1268 These methods return iterators to the beginnnig or
1272 const_iterator
begin() const;
1274 const_iterator
end() const;
1277 const_reverse_iterator
rbegin() const;
1278 reverse_iterator
rbegin();
1279 const_reverse_iterator
rend() const;
1280 reverse_iterator
rend();
1286 The supported STL functions are listed here. Please see any
1287 STL reference for their documentation.
1290 size_t length() const;
1291 size_type
size() const;
1292 size_type
max_size() const;
1293 size_type
capacity() const;
1294 void reserve(size_t sz
);
1296 void resize(size_t nSize
, wxUniChar ch
= '\0');
1298 wxString
& append(const wxString
& str
, size_t pos
, size_t n
);
1299 wxString
& append(const wxString
& str
);
1300 wxString
& append(const char *sz
, size_t n
);
1301 wxString
& append(const wchar_t *sz
, size_t n
);
1302 wxString
& append(size_t n
, wxUniChar ch
);
1303 wxString
& append(const_iterator first
, const_iterator last
);
1305 wxString
& assign(const wxString
& str
, size_t pos
, size_t n
);
1306 wxString
& assign(const wxString
& str
);
1307 wxString
& assign(const char *sz
, size_t n
);
1308 wxString
& assign(const wchar_t *sz
, size_t n
);
1309 wxString
& assign(size_t n
, wxUniChar ch
);
1310 wxString
& assign(const_iterator first
, const_iterator last
);
1314 int compare(const wxString
& str
) const;
1315 int compare(size_t nStart
, size_t nLen
, const wxString
& str
) const;
1316 int compare(size_t nStart
, size_t nLen
,
1317 const wxString
& str
, size_t nStart2
, size_t nLen2
) const;
1318 int compare(size_t nStart
, size_t nLen
,
1319 const char* sz
, size_t nCount
= npos
) const;
1320 int compare(size_t nStart
, size_t nLen
,
1321 const wchar_t* sz
, size_t nCount
= npos
) const;
1325 wxString
& erase(size_type pos
= 0, size_type n
= npos
);
1326 iterator
erase(iterator first
, iterator last
);
1327 iterator
erase(iterator first
);
1329 size_t find(const wxString
& str
, size_t nStart
= 0) const;
1330 size_t find(const char* sz
, size_t nStart
= 0, size_t n
= npos
) const;
1331 size_t find(const wchar_t* sz
, size_t nStart
= 0, size_t n
= npos
) const;
1332 size_t find(wxUniChar ch
, size_t nStart
= 0) const;
1334 wxString
& insert(size_t nPos
, const wxString
& str
);
1335 wxString
& insert(size_t nPos
, const wxString
& str
, size_t nStart
, size_t n
);
1336 wxString
& insert(size_t nPos
, const char *sz
, size_t n
);
1337 wxString
& insert(size_t nPos
, const wchar_t *sz
, size_t n
);
1338 wxString
& insert(size_t nPos
, size_t n
, wxUniChar ch
);
1339 iterator
insert(iterator it
, wxUniChar ch
);
1340 void insert(iterator it
, const_iterator first
, const_iterator last
);
1341 void insert(iterator it
, size_type n
, wxUniChar ch
);
1343 wxString
& replace(size_t nStart
, size_t nLen
, const wxString
& str
);
1344 wxString
& replace(size_t nStart
, size_t nLen
, size_t nCount
, wxUniChar ch
);
1345 wxString
& replace(size_t nStart
, size_t nLen
,
1346 const wxString
& str
, size_t nStart2
, size_t nLen2
);
1347 wxString
& replace(size_t nStart
, size_t nLen
,
1348 const char* sz
, size_t nCount
);
1349 wxString
& replace(size_t nStart
, size_t nLen
,
1350 const wchar_t* sz
, size_t nCount
);
1351 wxString
& replace(size_t nStart
, size_t nLen
,
1352 const wxString
& s
, size_t nCount
);
1353 wxString
& replace(iterator first
, iterator last
, const wxString
& s
);
1354 wxString
& replace(iterator first
, iterator last
, const char* s
, size_type n
);
1355 wxString
& replace(iterator first
, iterator last
, const wchar_t* s
, size_type n
);
1356 wxString
& replace(iterator first
, iterator last
, size_type n
, wxUniChar ch
);
1357 wxString
& replace(iterator first
, iterator last
,
1358 const_iterator first1
, const_iterator last1
);
1359 wxString
& replace(iterator first
, iterator last
,
1360 const char *first1
, const char *last1
);
1361 wxString
& replace(iterator first
, iterator last
,
1362 const wchar_t *first1
, const wchar_t *last1
);
1364 size_t rfind(const wxString
& str
, size_t nStart
= npos
) const;
1365 size_t rfind(const char* sz
, size_t nStart
= npos
, size_t n
= npos
) const;
1366 size_t rfind(const wchar_t* sz
, size_t nStart
= npos
, size_t n
= npos
) const;
1367 size_t rfind(wxUniChar ch
, size_t nStart
= npos
) const;
1369 wxString
substr(size_t nStart
= 0, size_t nLen
= npos
) const;
1371 void swap(wxString
& str
);
1387 wxString wxEmptyString
;
1393 @class wxStringBufferLength
1395 This tiny class allows you to conveniently access the wxString
1396 internal buffer as a writable pointer without any risk of forgetting to restore
1397 the string to the usable state later, and allows the user to set the internal
1398 length of the string.
1400 For example, assuming you have a low-level OS function called
1401 @c int GetMeaningOfLifeAsString(char *) copying the value in the provided
1402 buffer (which must be writable, of course), and returning the actual length
1403 of the string, you might call it like this:
1407 wxStringBuffer theAnswerBuffer(theAnswer, 1024);
1408 int nLength = GetMeaningOfLifeAsString(theAnswerBuffer);
1409 theAnswerBuffer.SetLength(nLength);
1410 if ( theAnswer != "42" )
1412 wxLogError("Something is very wrong!");
1416 Note that the exact usage of this depends on whether or not wxUSE_STL is
1417 enabled. If wxUSE_STL is enabled, wxStringBuffer creates a separate empty
1418 character buffer, and if wxUSE_STL is disabled, it uses GetWriteBuf() from
1419 wxString, keeping the same buffer wxString uses intact. In other words,
1420 relying on wxStringBuffer containing the old wxString data is not a good
1421 idea if you want to build your program both with and without wxUSE_STL.
1423 Note that SetLength @c must be called before wxStringBufferLength destructs.
1428 class wxStringBufferLength
1432 Constructs a writable string buffer object associated with the given string
1433 and containing enough space for at least @a len characters. Basically, this
1434 is equivalent to calling wxString::GetWriteBuf and
1437 wxStringBufferLength(const wxString
& str
, size_t len
);
1440 Restores the string passed to the constructor to the usable state by calling
1441 wxString::UngetWriteBuf on it.
1443 ~wxStringBufferLength();
1446 Sets the internal length of the string referred to by wxStringBufferLength to
1447 @a nLength characters.
1448 Must be called before wxStringBufferLength destructs.
1450 void SetLength(size_t nLength
);
1453 Returns the writable pointer to a buffer of the size at least equal to the
1454 length specified in the constructor.
1456 wxChar
* operator wxChar
*();