1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: interface of wxStringBuffer
4 // Author: wxWidgets team
6 // Licence: wxWindows license
7 /////////////////////////////////////////////////////////////////////////////
13 This tiny class allows to conveniently access the wxString
14 internal buffer as a writable pointer without any risk of forgetting to restore
15 the string to the usable state later.
17 For example, assuming you have a low-level OS function called
18 @c GetMeaningOfLifeAsString(char *) returning the value in the provided
19 buffer (which must be writable, of course) you might call it like this:
23 GetMeaningOfLifeAsString(wxStringBuffer(theAnswer, 1024));
24 if ( theAnswer != "42" )
26 wxLogError("Something is very wrong!");
30 Note that the exact usage of this depends on whether on not wxUSE_STL is
32 wxUSE_STL is enabled, wxStringBuffer creates a separate empty character buffer,
34 if wxUSE_STL is disabled, it uses GetWriteBuf() from wxString, keeping the same
36 wxString uses intact. In other words, relying on wxStringBuffer containing the
38 wxString data is probably not a good idea if you want to build your program in
40 with and without wxUSE_STL.
49 Constructs a writable string buffer object associated with the given string
50 and containing enough space for at least @a len characters. Basically, this
51 is equivalent to calling wxString::GetWriteBuf and
54 wxStringBuffer(const wxString
& str
, size_t len
);
57 Restores the string passed to the constructor to the usable state by calling
58 wxString::UngetWriteBuf on it.
63 Returns the writable pointer to a buffer of the size at least equal to the
64 length specified in the constructor.
66 wxStringCharType
* operator wxStringCharType
*();
75 wxString is a class representing a Unicode character string.
76 wxString uses @c std::string internally to store its content
77 unless this is not supported by the compiler or disabled
78 specifically when building wxWidgets. Therefore wxString
79 inherits many features from @c std::string's. Most
80 implementations of @std::string are thread-safe and don't
81 use reference counting. By default, wxString uses @c std::string
82 internally even if wxUSE_STL is not defined.
84 Since wxWidgets 3.0 wxString internally uses UCS-2 (basically 2-byte per
85 character wchar_t) under Windows and UTF-8 under Unix, Linux and
86 OS X to store its content. Much work has been done to make existing
87 code using ANSI string literals work as before. If you need to have a
88 wxString that uses wchar_t on Unix and Linux, too, you can specify
89 this on the command line with the @c configure @c --disable-utf8 switch.
91 As a consequence of this change, iterating over a wxString by index
92 can become inefficient in UTF8 mode and iterators should be used instead:
96 wxString::const_iterator i;
97 for (i = s.begin(); i != s.end(); ++i)
99 wxUniChar uni_ch = *i;
100 // do something with it
105 @ref overview_string "wxString overview" and the
106 @ref overview_unicode "Unicode overview" for more information
109 wxString implements most of the methods of the @c std::string class.
110 These standard functions are only listed here, but there are not
111 fully documented in this manual. Please see the STL documentation.
112 The behaviour of all these functions is identical to the behaviour
115 You may notice that wxString sometimes has many functions which do
116 the same thing like, for example, Length(), Len() and length() which
117 all return the string length. In all cases of such duplication the
118 @c std::string compatible method should be used.
120 Anything may be concatenated (appended to) with a string. However, you can't
121 append something to a C string (including literal constants), so to do this it
122 should be converted to a wxString first.
130 A string may be constructed either from a C string, (some number of copies of)
131 a single character or a wide (UNICODE) string. For all constructors (except the
132 default which creates an empty string) there is also a corresponding assignment
139 The MakeXXX() variants modify the string in place, while the other functions
140 return a new string which contains the original text converted to the upper or
141 lower case and leave the original string unchanged.
148 Many functions in this section take a character index in the string. As with C
149 strings and/or arrays, the indices start from 0, so the first character of a
150 string is string[0]. Attempt to access a character beyond the end of the
151 string (which may be even 0 if the string is empty) will provoke an assert
152 failure in @ref overview_debugging "debug build", but no checks are
153 done in release builds.
154 This section also contains both implicit and explicit conversions to C style
155 strings. Although implicit conversion is quite convenient, it is advised to use
156 explicit c_str() method for the sake of clarity.
159 @li GetWritableChar()
168 The default comparison function Cmp() is case-sensitive and
169 so is the default version of IsSameAs(). For case
170 insensitive comparisons you should use CmpNoCase() or
171 give a second parameter to IsSameAs. This last function is may be more
172 convenient if only equality of the strings matters because it returns a boolean
173 @true value if the strings are the same and not 0 (which is usually @false
175 Matches() is a poor man's regular expression matcher: it only understands
176 '*' and '?' metacharacters in the sense of DOS command line interpreter.
177 StartsWith() is helpful when parsing a line of text which should start
178 with some predefined prefix and is more efficient than doing direct string
179 comparison as you would also have to precalculate the length of the prefix then.
188 The string provides functions for conversion to signed and unsigned integer and
189 floating point numbers. All three functions take a pointer to the variable to
190 put the numeric value in and return @true if the @b entire string could be
191 converted to a number.
199 These are "advanced" functions and they will be needed quite rarely.
200 Alloc() and Shrink() are only interesting for optimization purposes.
201 wxStringBuffer and wxStringBufferLength classes may be very useful
202 when working with some external API which requires the caller to provide
208 @li wxStringBufferLength
210 Misc. other string functions.
216 These functions return the string length and check whether the string
217 is empty or empty it.
226 These functions allow to extract substring from this string. All of them don't
227 modify the original string and return a new string containing the extracted
241 These functions replace the standard @e strchr() and @e strstr()
247 Both formatted versions (Printf/() and stream-like insertion operators
248 exist (for basic types only). Additionally, the Format() function allows
249 to use simply append formatted value to a string:
257 These functions are deprecated, please consider using new wxWidgets 2.0
258 functions instead of them (or, even better, std::string compatible variants).
260 Contains(), First(), Freq(), IsAscii(), IsNull(),
261 IsNumber(), IsWord(), Last(), Length(), LowerCase(), Remove(), Strip(),
262 SubString(), UpperCase()
268 ::Objects:, ::wxEmptyString,
270 @see @ref overview_string "wxString overview", @ref overview_unicode
277 An 'invalid' value for string index
279 static const size_t npos
;
285 typedef wxUniChar value_type
;
286 typedef wxUniChar char_type
;
287 typedef wxUniCharRef reference
;
288 typedef wxChar
* pointer
;
289 typedef const wxChar
* const_pointer
;
290 typedef size_t size_type
;
291 typedef wxUniChar const_reference
;
300 Creates a string from another string. Just increases the ref
303 wxString(const wxString
& stringSrc
);
307 Constructs a string from the string literal @c psz using
308 the current locale encoding to convert it to Unicode.
310 wxString(const char *psz
);
313 Constructs a string from the string literal @c psz using
314 @c conv to convert it Unicode.
316 wxString(const char *psz
, const wxMBConv
& conv
);
319 Constructs a string from the first @ nLength character of the string literal @c psz using
320 the current locale encoding to convert it to Unicode.
322 wxString(const char *psz
, size_t nLength
);
325 Constructs a string from the first @ nLength character of the string literal @c psz using
326 @c conv to convert it Unicode.
328 wxString(const char *psz
, const wxMBConv
& conv
, size_t nLength
);
331 Constructs a string from the string literal @c pwz.
333 wxString(const wchar_t *pwz
);
336 Constructs a string from the first @ nLength characters of the string literal @c pwz.
338 wxString(const wchar_t *pwz
, size_t nLength
);
341 Constructs a string from @c buf using the using
342 the current locale encoding to convert it to Unicode.
344 wxString(const wxCharBuffer
& buf
);
347 Constructs a string from @c buf.
349 wxString(const wxWCharBuffer
& buf
);
352 Constructs a string from @str using the using
353 the current locale encoding to convert it to Unicode.
355 wxString(const std::string
& str
);
358 Constructs a string from @str.
360 wxString(const std::wstring
& str
);
364 String destructor. Note that this is not virtual, so wxString must not be
370 Gets all the characters after the first occurrence of @e ch.
371 Returns the empty string if @a ch is not found.
373 wxString
AfterFirst(wxUniChar ch
) const;
376 Gets all the characters after the last occurrence of @e ch.
377 Returns the whole string if @a ch is not found.
379 wxString
AfterLast(wxUniChar ch
) const;
382 Preallocate enough space for wxString to store @a nLen characters.
384 Please note that this method does the same thing as the standard
385 reserve() one and shouldn't be used in new code.
387 This function may be used to increase speed when the string is
388 constructed by repeated concatenation as in
391 // delete all vowels from the string
392 wxString DeleteAllVowels(const wxString& original)
396 size_t len = original.length();
400 for ( size_t n = 0; n < len; n++ )
402 if ( strchr("aeuio", tolower(original[n])) == NULL )
403 result += original[n];
410 because it will avoid the need to reallocate string memory many times
411 (in case of long strings). Note that it does not set the maximal length
412 of a string -- it will still expand if more than @a nLen characters are
413 stored in it. Also, it does not truncate the existing string (use
414 Truncate() for this) even if its current length is greater than @a nLen.
416 @return @true if memory was successfully allocated, @false otherwise.
418 bool Alloc(size_t nLen
);
422 Appends the string or string literal or character.
424 wxString
& Append(const char* psz
, size_t nLen
);
425 wxString
& Append(const wchar_t* pwz
, size_t nLen
)
426 wxString
&Append(const wxString
&s
);
427 wxString
&Append(wxUniChar ch
, size_t count
= 1u);
431 Gets all characters before the first occurrence of @e ch.
432 Returns the whole string if @a ch is not found.
434 wxString
BeforeFirst(wxUniChar ch
) const;
437 Gets all characters before the last occurrence of @e ch.
438 Returns the empty string if @a ch is not found.
440 wxString
BeforeLast(wxUniChar ch
) const;
444 Empties the string and frees memory occupied by it.
450 Returns a deep copy of the string.
452 That is, the returned string is guaranteed to not share data with this
453 string when using reference-counted wxString implementation.
455 This method is primarily useful for passing strings between threads
456 (because wxString is not thread-safe). Unlike creating a copy using
457 @c wxString(c_str()), Clone() handles embedded NULs correctly.
461 wxString
Clone() const;
464 Case-sensitive comparison.
465 Returns a positive value if the string is greater than the argument,
466 zero if it is equal to it or a negative value if it is less than the
467 argument (same semantics as the standard @e strcmp() function).
469 See also CmpNoCase(), IsSameAs().
471 int Cmp(const wxString
& s
) const;
474 Case-insensitive comparison.
475 Returns a positive value if the string is greater than the argument,
476 zero if it is equal to it or a negative value if it is less than the
477 argument (same semantics as the standard @e strcmp() function).
479 See also Cmp(), IsSameAs().
481 int CmpNoCase(const wxString
& s
) const;
488 bool operator ==(const wxString
& x
, const wxString
& y
);
489 bool operator ==(const wxString
& x
, wxUniChar ch
);
490 bool operator !=(const wxString
& x
, const wxString
& y
);
491 bool operator !=(const wxString
& x
, wxUniChar ch
);
492 bool operator(const wxString
& x
, const wxString
& y
);
493 bool operator(const wxString
& x
, wxUniChar ch
);
494 bool operator =(const wxString
& x
, const wxString
& y
);
495 bool operator =(const wxString
& x
, wxUniChar ch
);
496 bool operator(const wxString
& x
, const wxString
& y
);
497 bool operator(const wxString
& x
, wxUniChar ch
);
498 bool operator =(const wxString
& x
, const wxString
& y
);
499 bool operator =(const wxString
& x
, wxUniChar ch
);
504 Returns @true if target appears anywhere in wxString; else @false.
505 This is a wxWidgets 1.xx compatibility function; you should not use it in new
508 bool Contains(const wxString
& str
) const;
512 Makes the string empty, but doesn't free memory occupied by the string.
518 This function can be used to test if the string ends with the specified
519 @e suffix. If it does, the function will return @true and put the
520 beginning of the string before the suffix into @a rest string if it is not
521 @NULL. Otherwise, the function returns @false and doesn't
524 bool EndsWith(const wxString
& suffix
, wxString rest
= NULL
) const;
528 Searches for the given string. Returns the starting index, or
529 @c wxNOT_FOUND if not found.
531 int Find(wxUniChar ch
, bool fromEnd
= false) const;
532 int Find(const wxString
& sub
) const;
538 This is a wxWidgets 1.xx compatibility function;
539 you should not use it in new code.
541 int First(wxUniChar ch
) const;
542 int First(const wxString
& str
) const;
546 This static function returns the string containing the result of calling
547 Printf() with the passed parameters on it.
549 @see FormatV(), Printf()
551 static wxString
Format(const wxChar format
, ...);
554 This static function returns the string containing the result of calling
555 PrintfV() with the passed parameters on it.
557 @see Format(), PrintfV()
559 static wxString
FormatV(const wxChar format
, va_list argptr
);
562 Returns the number of occurrences of @a ch in the string.
563 This is a wxWidgets 1.xx compatibility function; you should not use it in new
566 int Freq(wxUniChar ch
) const;
570 Converts given buffer of binary data from 8-bit string to wxString. In
571 Unicode build, the string is interpreted as being in ISO-8859-1
572 encoding. The version without @a len parameter takes NUL-terminated
575 This is a convenience method useful when storing binary data in
576 wxString. It should be used @em only for that purpose and only in
577 conjunction with To8BitData(). Use mb_str() for conversion of character
578 data to known encoding.
582 @see wxString::To8BitData()
584 static wxString
From8BitData(const char* buf
, size_t len
);
585 static wxString
From8BitData(const char* buf
);
590 Converts the string or character from an ASCII, 7-bit form
591 to the native wxString representation.
593 static wxString
FromAscii(const char* s
);
594 static wxString
FromAscii(const unsigned char* s
);
595 static wxString
FromAscii(const char* s
, size_t len
);
596 static wxString
FromAscii(const unsigned char* s
, size_t len
);
597 static wxString
FromAscii(char c
);
602 Converts C string encoded in UTF-8 to wxString.
603 Note that this method assumes that @a s is a valid UTF-8 sequence and
604 doesn't do any validation in release builds, it's validity is only checked in
607 static wxString
FromUTF8(const char* s
);
608 static wxString
FromUTF8(const char* s
, size_t len
);
612 Returns the character at position @a n (read-only).
614 wxUniChar
GetChar(size_t n
) const;
617 wxWidgets compatibility conversion. Same as c_str().
619 const wxCStrData
* GetData() const;
622 Returns a reference to the character at position @e n.
624 wxUniCharRef
GetWritableChar(size_t n
);
627 Returns a writable buffer of at least @a len bytes.
628 It returns a pointer to a new memory block, and the
629 existing data will not be copied.
630 Call UngetWriteBuf() as soon as possible to put the
631 string back into a reasonable state.
632 This method is deprecated, please use wxStringBuffer or
633 wxStringBufferLength instead.
635 wxStringCharType
* GetWriteBuf(size_t len
);
638 Returns @true if the string contains only ASCII characters.
639 This is a wxWidgets 1.xx compatibility function; you should not use it in new
642 bool IsAscii() const;
645 Returns @true if the string is empty.
647 bool IsEmpty() const;
650 Returns @true if the string is empty (same as wxString::IsEmpty).
651 This is a wxWidgets 1.xx compatibility function; you should not use it in new
657 Returns @true if the string is an integer (with possible sign).
658 This is a wxWidgets 1.xx compatibility function; you should not use it in new
661 bool IsNumber() const;
665 Test whether the string is equal to the single character @e c. The test is
666 case-sensitive if @a caseSensitive is @true (default) or not if it is @c
668 Returns @true if the string is equal to the character, @false otherwise.
669 See also Cmp(), CmpNoCase()
671 bool IsSameAs(const wxString
&s
, bool caseSensitive
= true) const;
672 bool IsSameAs(wxUniChar ch
, bool caseSensitive
= true) const;
676 Returns @true if the string is a word.
677 This is a wxWidgets 1.xx compatibility function; you should not use it in new
684 Returns a reference to the last character (writable).
685 This is a wxWidgets 1.xx compatibility function;
686 you should not use it in new code.
689 const wxUniChar
Last();
693 Returns the first @a count characters of the string.
695 wxString
Left(size_t count
) const;
698 Returns the length of the string.
703 Returns the length of the string (same as Len).
704 This is a wxWidgets 1.xx compatibility function; you should not use it in new
707 size_t Length() const;
710 Returns this string converted to the lower case.
712 wxString
Lower() const;
716 This is a wxWidgets 1.xx compatibility function; you should not use it in new
722 Converts all characters to lower case and returns the result.
724 wxString
& MakeLower();
727 Converts all characters to upper case and returns the result.
729 wxString
& MakeUpper();
732 Returns @true if the string contents matches a mask containing '*' and '?'.
734 bool Matches(const wxString
& mask
) const;
737 Returns a substring starting at @e first, with length @e count, or the rest of
738 the string if @a count is the default value.
740 wxString
Mid(size_t first
, size_t count
= wxSTRING_MAXLEN
) const;
744 Adds @a count copies of @a pad to the beginning, or to the end of the
745 string (the default). Removes spaces from the left or from the right (default).
747 wxString
& Pad(size_t count
, wxUniChar pad
= ' ',
748 bool fromRight
= true);
751 Prepends @a str to this string, returning a reference to this string.
753 wxString
& Prepend(const wxString
& str
);
756 Similar to the standard function @e sprintf(). Returns the number of
757 characters written, or an integer less than zero on error.
758 Note that if @c wxUSE_PRINTF_POS_PARAMS is set to 1, then this function supports
759 Unix98-style positional parameters:
761 @note This function will use a safe version of @e vsprintf() (usually called
762 @e vsnprintf()) whenever available to always allocate the buffer of correct
763 size. Unfortunately, this function is not available on all platforms and the
764 dangerous @e vsprintf() will be used then which may lead to buffer overflows.
766 int Printf(const wxChar
* pszFormat
, ...);
769 Similar to vprintf. Returns the number of characters written, or an integer
773 int PrintfV(const wxChar
* pszFormat
, va_list argPtr
);
777 Removes @a len characters from the string, starting at @e pos.
778 This is a wxWidgets 1.xx compatibility function; you should not use it in new
781 wxString
Remove(size_t pos
);
782 wxString
Remove(size_t pos
, size_t len
);
786 Removes the last character.
788 wxString
RemoveLast();
791 Replace first (or all) occurrences of substring with another one.
792 @e replaceAll: global replace (default), or only the first occurrence.
793 Returns the number of replacements made.
795 size_t Replace(const wxString
& strOld
, const wxString
& strNew
,
796 bool replaceAll
= true);
799 Returns the last @a count characters.
801 wxString
Right(size_t count
) const;
804 Sets the character at position @e n.
806 void SetChar(size_t n
, wxUniChar ch
);
809 Minimizes the string's memory. This can be useful after a call to
810 Alloc() if too much memory were preallocated.
815 This function can be used to test if the string starts with the specified
816 @e prefix. If it does, the function will return @true and put the rest
817 of the string (i.e. after the prefix) into @a rest string if it is not
818 @NULL. Otherwise, the function returns @false and doesn't modify the
821 bool StartsWith(const wxString
& prefix
, wxString rest
= NULL
) const;
824 Strip characters at the front and/or end. The same as Trim except that it
825 doesn't change this string.
826 This is a wxWidgets 1.xx compatibility function; you should not use it in new
829 wxString
Strip(stripType s
= trailing
) const;
832 Returns the part of the string between the indices @a from and @e to
834 This is a wxWidgets 1.xx compatibility function, use Mid()
835 instead (but note that parameters have different meaning).
837 wxString
SubString(size_t from
, size_t to
) const;
841 Converts the string to an 8-bit string in ISO-8859-1 encoding in the
842 form of a wxCharBuffer (Unicode builds only).
844 This is a convenience method useful when storing binary data in
845 wxString. It should be used @em only for this purpose. It is only valid
846 to call this method on strings created using From8BitData().
850 @see wxString::From8BitData()
852 const char* To8BitData() const;
853 const const wxCharBuffer
To8BitData() const;
858 Converts the string to an ASCII, 7-bit string in the form of
859 a wxCharBuffer (Unicode builds only) or a C string (ANSI builds).
860 Note that this conversion only works if the string contains only ASCII
861 characters. The @ref mbstr() mb_str method provides more
862 powerful means of converting wxString to C string.
864 const char* ToAscii() const;
865 const const wxCharBuffer
ToAscii() const;
869 Attempts to convert the string to a floating point number. Returns @true on
870 success (the number is stored in the location pointed to by @e val) or @false
871 if the string does not represent such number (the value of @a val is not
872 modified in this case).
874 @see ToLong(), ToULong()
876 bool ToDouble(double val
) const;
879 Attempts to convert the string to a signed integer in base @e base. Returns
880 @true on success in which case the number is stored in the location
881 pointed to by @a val or @false if the string does not represent a
882 valid number in the given base (the value of @a val is not modified
884 The value of @a base must be comprised between 2 and 36, inclusive, or
885 be a special value 0 which means that the usual rules of @c C numbers are
886 applied: if the number starts with @c 0x it is considered to be in base
887 16, if it starts with @c 0 - in base 8 and in base 10 otherwise. Note
888 that you may not want to specify the base 0 if you are parsing the numbers
889 which may have leading zeroes as they can yield unexpected (to the user not
890 familiar with C) results.
892 @see ToDouble(), ToULong()
894 bool ToLong(long val
, int base
= 10) const;
897 This is exactly the same as ToLong() but works with 64
899 Notice that currently it doesn't work (always returns @false) if parsing of 64
900 bit numbers is not supported by the underlying C run-time library. Compilers
901 with C99 support and Microsoft Visual C++ version 7 and higher do support this.
903 @see ToLong(), ToULongLong()
905 bool ToLongLong(wxLongLong_t val
, int base
= 10) const;
908 Attempts to convert the string to an unsigned integer in base @e base.
909 Returns @true on success in which case the number is stored in the
910 location pointed to by @a val or @false if the string does not
911 represent a valid number in the given base (the value of @a val is not
912 modified in this case). Please notice that this function
913 behaves in the same way as the standard @c strtoul() and so it simply
914 converts negative numbers to unsigned representation instead of rejecting them
915 (e.g. -1 is returned as @c ULONG_MAX).
916 See ToLong() for the more detailed
917 description of the @a base parameter.
919 @see ToDouble(), ToLong()
921 bool ToULong(unsigned long val
, int base
= 10) const;
924 This is exactly the same as ToULong() but works with 64
926 Please see ToLongLong() for additional remarks.
928 bool ToULongLong(wxULongLong_t val
, int base
= 10) const;
934 const char* ToUTF8() const;
935 const const wxCharBuffer
ToUF8() const;
939 Removes white-space (space, tabs, form feed, newline and carriage return) from
940 the left or from the right end of the string (right is default).
942 wxString
& Trim(bool fromRight
= true);
945 Truncate the string to the given length.
947 wxString
& Truncate(size_t len
);
951 Puts the string back into a reasonable state (in which it can be used
953 GetWriteBuf() was called.
954 The version of the function without the @a len parameter will calculate the
955 new string length itself assuming that the string is terminated by the first
956 @c NUL character in it while the second one will use the specified length
957 and thus is the only version which should be used with the strings with
958 embedded @c NULs (it is also slightly more efficient as @c strlen()
959 doesn't have to be called).
960 This method is deprecated, please use
962 wxStringBufferLength instead.
964 void UngetWriteBuf();
965 void UngetWriteBuf(size_t len
);
969 Returns this string converted to upper case.
971 wxString
Upper() const;
974 The same as MakeUpper.
975 This is a wxWidgets 1.xx compatibility function; you should not use it in new
981 Returns a pointer to the string data (@c const char* when using UTF-8
982 internally, @c const wchar_t* when using UCS-2 internally).
984 Note that the returned value is not convertible to @c char* or
985 @c wchar_t*, use char_str() or wchar_str() if you need to pass
986 string value to a function expecting non-const pointer.
988 const wxCStrData
c_str() const;
991 Returns an object with string data that is implicitly convertible to
992 @c char* pointer. Note that any change to the returned buffer is lost and so
993 this function is only usable for passing strings to legacy libraries that
994 don't have const-correct API. Use wxStringBuffer if you want to modify
999 wxWritableCharBuffer
char_str(const wxMBConv
& conv
= wxConvLibc
) const;
1003 Returns string representation suitable for passing to OS' functions
1006 const wchar_t* fn_str() const;
1007 const char* fn_str() const;
1008 const wxCharBuffer
fn_str() const;
1013 Returns multibyte (C string) representation of the string.
1014 In Unicode build, converts using @e conv's wxMBConv::cWC2MB
1015 method and returns wxCharBuffer. In ANSI build, this function
1017 The macro wxWX2MBbuf is defined as the correct return type (without const).
1019 @see wxMBConv, c_str(), wc_str(), fn_str(), char_str()
1021 const char* mb_str(const wxMBConv
& conv
= wxConvLibc
) const;
1022 const const wxCharBuffer
mb_str(const wxMBConv
& conv
= wxConvLibc
) const;
1026 Extraction from a stream.
1028 friend istream
operator(istream
& is
, wxString
& str
);
1032 These functions work as C++ stream insertion operators: they insert the given
1033 value into the string. Precision or format cannot be set using them, you can
1034 use Printf() for this.
1036 wxString
operator(const wxString
& str
);
1037 wxString
operator(wxUniChar ch
);
1038 wxString
operator(int i
);
1039 wxString
operator(float f
);
1040 wxString
operator(double d
);
1044 Same as Mid (substring extraction).
1046 wxString
operator ()(size_t start
, size_t len
);
1050 Concatenation: these operators return a new string equal to the
1051 concatenation of the operands.
1053 wxString
operator +(const wxString
& x
, const wxString
& y
);
1054 wxString
operator +(const wxString
& x
, wxUniChar y
);
1059 Concatenation in place: the argument is appended to the string.
1061 void operator +=(const wxString
& str
);
1062 void operator +=(wxUniChar c
);
1067 Assignment: the effect of each operation is the same as for the corresponding
1068 constructor (see @ref construct() "wxString constructors").
1070 wxString
operator =(const wxString
& str
);
1071 wxString
operator =(wxUniChar c
);
1078 wxUniChar
operator [](size_t i
) const;
1079 wxUniCharRef
operator [](size_t i
);
1083 Empty string is @false, so !string will only return @true if the
1088 bool operator!() const;
1093 Converts the strings contents to UTF-8 and returns it either as a
1094 temporary wxCharBuffer object or as a pointer to the internal
1095 string contents in UTF-8 build.
1097 const char* utf8_str() const;
1098 const wxCharBuffer
utf8_str() const;
1103 Converts the strings contents to the wide character represention
1104 and returns it as a temporary wxWCharBuffer object or returns a
1105 pointer to the internal string contents in wide character mode.
1107 The macro wxWX2WCbuf is defined as the correct return
1108 type (without const).
1110 @see wxMBConv, c_str(), mb_str(), fn_str(), wchar_str()
1112 const wchar_t* wc_str() const;
1113 const wxWCharBuffer
wc_str() const;
1117 Returns an object with string data that is implicitly convertible to
1118 @c char* pointer. Note that changes to the returned buffer may or may
1119 not be lost (depending on the build) and so this function is only usable for
1120 passing strings to legacy libraries that don't have const-correct API. Use
1121 wxStringBuffer if you want to modify the string.
1123 @see mb_str(), wc_str(), fn_str(), c_str(), char_str()
1125 wxWritableWCharBuffer
wchar_str() const;
1128 @name Iterator interface
1130 These methods return iterators to the beginnnig or
1134 const_iterator
begin() const;
1136 const_iterator
end() const;
1139 const_reverse_iterator
rbegin() const;
1140 reverse_iterator
rbegin();
1141 const_reverse_iterator
rend() const;
1142 reverse_iterator
rend();
1148 The supported STL functions are listed here. Please see any
1149 STL reference for their documentation.
1152 size_t length() const;
1153 size_type
size() const;
1154 size_type
max_size() const;
1155 size_type
capacity() const;
1156 void reserve(size_t sz
);
1158 void resize(size_t nSize
, wxUniChar ch
= '\0');
1160 wxString
& append(const wxString
& str
, size_t pos
, size_t n
);
1161 wxString
& append(const wxString
& str
);
1162 wxString
& append(const char *sz
, size_t n
);
1163 wxString
& append(const wchar_t *sz
, size_t n
);
1164 wxString
& append(size_t n
, wxUniChar ch
);
1165 wxString
& append(const_iterator first
, const_iterator last
);
1167 wxString
& assign(const wxString
& str
, size_t pos
, size_t n
);
1168 wxString
& assign(const wxString
& str
);
1169 wxString
& assign(const char *sz
, size_t n
);
1170 wxString
& assign(const wchar_t *sz
, size_t n
);
1171 wxString
& assign(size_t n
, wxUniChar ch
);
1172 wxString
& assign(const_iterator first
, const_iterator last
);
1176 int compare(const wxString
& str
) const;
1177 int compare(size_t nStart
, size_t nLen
, const wxString
& str
) const;
1178 int compare(size_t nStart
, size_t nLen
,
1179 const wxString
& str
, size_t nStart2
, size_t nLen2
) const;
1180 int compare(size_t nStart
, size_t nLen
,
1181 const char* sz
, size_t nCount
= npos
) const;
1182 int compare(size_t nStart
, size_t nLen
,
1183 const wchar_t* sz
, size_t nCount
= npos
) const;
1187 wxString
& erase(size_type pos
= 0, size_type n
= npos
);
1188 iterator
erase(iterator first
, iterator last
);
1189 iterator
erase(iterator first
);
1191 size_t find(const wxString
& str
, size_t nStart
= 0) const;
1192 size_t find(const char* sz
, size_t nStart
= 0, size_t n
= npos
) const;
1193 size_t find(const wchar_t* sz
, size_t nStart
= 0, size_t n
= npos
) const;
1194 size_t find(wxUniChar ch
, size_t nStart
= 0) const;
1196 wxString
& insert(size_t nPos
, const wxString
& str
);
1197 wxString
& insert(size_t nPos
, const wxString
& str
, size_t nStart
, size_t n
);
1198 wxString
& insert(size_t nPos
, const char *sz
, size_t n
);
1199 wxString
& insert(size_t nPos
, const wchar_t *sz
, size_t n
);
1200 wxString
& insert(size_t nPos
, size_t n
, wxUniChar ch
);
1201 iterator
insert(iterator it
, wxUniChar ch
);
1202 void insert(iterator it
, const_iterator first
, const_iterator last
);
1203 void insert(iterator it
, size_type n
, wxUniChar ch
);
1205 wxString
& replace(size_t nStart
, size_t nLen
, const wxString
& str
);
1206 wxString
& replace(size_t nStart
, size_t nLen
, size_t nCount
, wxUniChar ch
);
1207 wxString
& replace(size_t nStart
, size_t nLen
,
1208 const wxString
& str
, size_t nStart2
, size_t nLen2
);
1209 wxString
& replace(size_t nStart
, size_t nLen
,
1210 const char* sz
, size_t nCount
);
1211 wxString
& replace(size_t nStart
, size_t nLen
,
1212 const wchar_t* sz
, size_t nCount
);
1213 wxString
& replace(size_t nStart
, size_t nLen
,
1214 const wxString
& s
, size_t nCount
);
1215 wxString
& replace(iterator first
, iterator last
, const wxString
& s
);
1216 wxString
& replace(iterator first
, iterator last
, const char* s
, size_type n
);
1217 wxString
& replace(iterator first
, iterator last
, const wchar_t* s
, size_type n
);
1218 wxString
& replace(iterator first
, iterator last
, size_type n
, wxUniChar ch
);
1219 wxString
& replace(iterator first
, iterator last
,
1220 const_iterator first1
, const_iterator last1
);
1221 wxString
& replace(iterator first
, iterator last
,
1222 const char *first1
, const char *last1
);
1223 wxString
& replace(iterator first
, iterator last
,
1224 const wchar_t *first1
, const wchar_t *last1
);
1226 size_t rfind(const wxString
& str
, size_t nStart
= npos
) const;
1227 size_t rfind(const char* sz
, size_t nStart
= npos
, size_t n
= npos
) const;
1228 size_t rfind(const wchar_t* sz
, size_t nStart
= npos
, size_t n
= npos
) const;
1229 size_t rfind(wxUniChar ch
, size_t nStart
= npos
) const;
1231 wxString
substr(size_t nStart
= 0, size_t nLen
= npos
) const;
1233 void swap(wxString
& str
);
1249 wxString wxEmptyString
;
1255 @class wxStringBufferLength
1258 This tiny class allows to conveniently access the wxString
1259 internal buffer as a writable pointer without any risk of forgetting to restore
1260 the string to the usable state later, and allows the user to set the internal
1261 length of the string.
1263 For example, assuming you have a low-level OS function called
1264 @c int GetMeaningOfLifeAsString(char *) copying the value in the provided
1265 buffer (which must be writable, of course), and returning the actual length
1266 of the string, you might call it like this:
1270 wxStringBuffer theAnswerBuffer(theAnswer, 1024);
1271 int nLength = GetMeaningOfLifeAsString(theAnswerBuffer);
1272 theAnswerBuffer.SetLength(nLength);
1273 if ( theAnswer != "42" )
1275 wxLogError("Something is very wrong!");
1279 Note that the exact usage of this depends on whether on not wxUSE_STL is
1281 wxUSE_STL is enabled, wxStringBuffer creates a separate empty character buffer,
1283 if wxUSE_STL is disabled, it uses GetWriteBuf() from wxString, keeping the same
1285 wxString uses intact. In other words, relying on wxStringBuffer containing the
1287 wxString data is probably not a good idea if you want to build your program in
1289 with and without wxUSE_STL.
1291 Note that SetLength @c must be called before wxStringBufferLength destructs.
1296 class wxStringBufferLength
1300 Constructs a writable string buffer object associated with the given string
1301 and containing enough space for at least @a len characters. Basically, this
1302 is equivalent to calling wxString::GetWriteBuf and
1305 wxStringBufferLength(const wxString
& str
, size_t len
);
1308 Restores the string passed to the constructor to the usable state by calling
1309 wxString::UngetWriteBuf on it.
1311 ~wxStringBufferLength();
1314 Sets the internal length of the string referred to by wxStringBufferLength to
1315 @a nLength characters.
1316 Must be called before wxStringBufferLength destructs.
1318 void SetLength(size_t nLength
);
1321 Returns the writable pointer to a buffer of the size at least equal to the
1322 length specified in the constructor.
1324 wxChar
* operator wxChar
*();