]>
git.saurik.com Git - wxWidgets.git/blob - interface/string.h
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 wxChar
* operator wxChar
*();
75 wxString is a class representing a character string. It uses
76 reference counting and copy-on-write internally and is not
77 thread-safe. Please see the
78 @ref overview_string "wxString overview" and the
79 @ref overview_unicode "Unicode overview" for more information
82 Since wxWidgets 3.0 internally uses UCS-2 (basically 2-byte per
83 character wchar_t) under Windows and UTF-8 under Unix, Linux and
84 OS X to store its content. Much work has been done to make
85 existing code using ANSI string literals work as before.
87 wxString implements most of the methods of the
88 std::string class. These standard functions are not documented in
89 this manual, please see the STL documentation. The behaviour of
90 all these functions is identical to the behaviour described there.
92 You may notice that wxString sometimes has many functions which do
93 the same thing like, for example, wxString::Length, wxString::Len and @c length()
94 which all return the string length. In all cases of such duplication the @c std::string
95 compatible method (@c length() in this case, always the lowercase version) should be
96 used as it will ensure smoother transition to @c std::string when wxWidgets
97 starts using it instead of wxString.
103 ::Objects:, ::wxEmptyString,
105 @see @ref overview_string "wxString overview", @ref overview_unicode
117 Creates a string from another string. Just increases the ref
120 wxString(const wxString
& stringSrc
);
124 Constructs a string from the string literal @c psz using
125 the current locale encoding to convert it to Unicode.
127 wxString(const char *psz
);
130 Constructs a string from the string literal @c psz using
131 @c conv to convert it Unicode.
133 wxString(const char *psz
, const wxMBConv
& conv
);
136 Constructs a string from the first @ nLength character of the string literal @c psz using
137 the current locale encoding to convert it to Unicode.
139 wxString(const char *psz
, size_t nLength
);
142 Constructs a string from the first @ nLength character of the string literal @c psz using
143 @c conv to convert it Unicode.
145 wxString(const char *psz
, const wxMBConv
& conv
, size_t nLength
);
148 Constructs a string from the string literal @c pwz.
150 wxString(const wchar_t *pwz
);
153 Constructs a string from the first @ nLength characters of the string literal @c pwz.
155 wxString(const wchar_t *pwz
, size_t nLength
);
158 Constructs a string from @c buf using the using
159 the current locale encoding to convert it to Unicode.
161 wxString(const wxCharBuffer
& buf
);
164 Constructs a string from @c buf.
166 wxString(const wxWCharBuffer
& buf
);
169 Constructs a string from @str using the using
170 the current locale encoding to convert it to Unicode.
172 wxString(const std::string
& str
);
175 Constructs a string from @str.
177 wxString(const std::wstring
& str
);
181 String destructor. Note that this is not virtual, so wxString must not be
187 Gets all the characters after the first occurrence of @e ch.
188 Returns the empty string if @a ch is not found.
190 wxString
AfterFirst(wxChar ch
) const;
193 Gets all the characters after the last occurrence of @e ch.
194 Returns the whole string if @a ch is not found.
196 wxString
AfterLast(wxChar ch
) const;
199 Preallocate enough space for wxString to store @a nLen characters.
201 Please note that this method does the same thing as the standard
202 reserve() one and shouldn't be used in new code.
204 This function may be used to increase speed when the string is
205 constructed by repeated concatenation as in
208 // delete all vowels from the string
209 wxString DeleteAllVowels(const wxString& original)
213 size_t len = original.length();
217 for ( size_t n = 0; n < len; n++ )
219 if ( strchr("aeuio", tolower(original[n])) == NULL )
220 result += original[n];
227 because it will avoid the need to reallocate string memory many times
228 (in case of long strings). Note that it does not set the maximal length
229 of a string -- it will still expand if more than @a nLen characters are
230 stored in it. Also, it does not truncate the existing string (use
231 Truncate() for this) even if its current length is greater than @a nLen.
233 @return @true if memory was successfully allocated, @false otherwise.
235 bool Alloc(size_t nLen
);
239 Concatenates character @a ch to this string, @a count times, returning a
243 wxString
Append(const wxChar
* psz
);
244 wxString
Append(wxChar ch
, int count
= 1);
248 Gets all characters before the first occurrence of @e ch.
249 Returns the whole string if @a ch is not found.
251 wxString
BeforeFirst(wxChar ch
) const;
254 Gets all characters before the last occurrence of @e ch.
255 Returns the empty string if @a ch is not found.
257 wxString
BeforeLast(wxChar ch
) const;
260 The MakeXXX() variants modify the string in place, while the other functions
261 return a new string which contains the original text converted to the upper or
262 lower case and leave the original string unchanged.
274 Many functions in this section take a character index in the string. As with C
275 strings and/or arrays, the indices start from 0, so the first character of a
276 string is string[0]. Attempt to access a character beyond the end of the
277 string (which may be even 0 if the string is empty) will provoke an assert
278 failure in @ref overview_debuggingoverview "debug build", but no checks are
281 This section also contains both implicit and explicit conversions to C style
282 strings. Although implicit conversion is quite convenient, it is advised to use
283 explicit @ref cstr() c_str method for the sake of clarity. Also
284 see overview() for the cases where it is necessary to
294 @ref operatorbracket() "operator []"
304 @ref operatorconstcharpt() "operator const char*"
309 Empties the string and frees memory occupied by it.
315 Returns a deep copy of the string.
317 That is, the returned string is guaranteed to not share data with this
318 string when using reference-counted wxString implementation.
320 This method is primarily useful for passing strings between threads
321 (because wxString is not thread-safe). Unlike creating a copy using
322 @c wxString(c_str()), Clone() handles embedded NULs correctly.
326 wxString
Clone() const;
330 Case-sensitive comparison.
331 Returns a positive value if the string is greater than the argument, zero if
332 it is equal to it or a negative value if it is less than the argument (same
334 as the standard @e strcmp() function).
335 See also CmpNoCase(), IsSameAs().
337 int Cmp(const wxString
& s
) const;
338 const int Cmp(const wxChar
* psz
) const;
343 Case-insensitive comparison.
344 Returns a positive value if the string is greater than the argument, zero if
345 it is equal to it or a negative value if it is less than the argument (same
347 as the standard @e strcmp() function).
348 See also Cmp(), IsSameAs().
350 int CmpNoCase(const wxString
& s
) const;
351 const int CmpNoCase(const wxChar
* psz
) const;
355 Case-sensitive comparison. Returns 0 if equal, 1 if greater or -1 if less.
356 This is a wxWidgets 1.xx compatibility function; use Cmp() instead.
358 int CompareTo(const wxChar
* psz
, caseCompare cmp
= exact
) const;
361 The default comparison function Cmp() is case-sensitive and
362 so is the default version of IsSameAs(). For case
363 insensitive comparisons you should use CmpNoCase() or
364 give a second parameter to IsSameAs. This last function is may be more
365 convenient if only equality of the strings matters because it returns a boolean
366 @true value if the strings are the same and not 0 (which is usually @false in
369 Matches() is a poor man's regular expression matcher:
370 it only understands '*' and '?' metacharacters in the sense of DOS command line
372 StartsWith() is helpful when parsing a line of
373 text which should start with some predefined prefix and is more efficient than
374 doing direct string comparison as you would also have to precalculate the
375 length of the prefix then.
394 bool operator ==(const wxString
& x
, const wxString
& y
);
395 bool operator ==(const wxString
& x
, const wxChar
* t
);
396 bool operator !=(const wxString
& x
, const wxString
& y
);
397 bool operator !=(const wxString
& x
, const wxChar
* t
);
398 bool operator(const wxString
& x
, const wxString
& y
);
399 bool operator(const wxString
& x
, const wxChar
* t
);
400 bool operator =(const wxString
& x
, const wxString
& y
);
401 bool operator =(const wxString
& x
, const wxChar
* t
);
402 bool operator(const wxString
& x
, const wxString
& y
);
403 bool operator(const wxString
& x
, const wxChar
* t
);
404 bool operator =(const wxString
& x
, const wxString
& y
);
405 bool operator =(const wxString
& x
, const wxChar
* t
);
409 Anything may be concatenated (appended to) with a string. However, you can't
410 append something to a C string (including literal constants), so to do this it
411 should be converted to a wxString first.
412 @ref operatorout() "operator "
414 @ref plusequal() "operator +="
416 @ref operatorplus() "operator +"
425 A string may be constructed either from a C string, (some number of copies of)
426 a single character or a wide (UNICODE) string. For all constructors (except the
427 default which creates an empty string) there is also a corresponding assignment
429 @ref construct() wxString
431 @ref operatorassign() "operator ="
433 @ref destruct() ~wxString
438 Returns @true if target appears anywhere in wxString; else @false.
439 This is a wxWidgets 1.xx compatibility function; you should not use it in new
442 bool Contains(const wxString
& str
) const;
445 The string provides functions for conversion to signed and unsigned integer and
446 floating point numbers. All three functions take a pointer to the variable to
447 put the numeric value in and return @true if the @b entire string could be
448 converted to a number.
462 Makes the string empty, but doesn't free memory occupied by the string.
468 This function can be used to test if the string ends with the specified
469 @e suffix. If it does, the function will return @true and put the
470 beginning of the string before the suffix into @a rest string if it is not
471 @NULL. Otherwise, the function returns @false and doesn't
474 bool EndsWith(const wxString
& suffix
, wxString rest
= NULL
) const;
478 Searches for the given string. Returns the starting index, or @c wxNOT_FOUND if
481 int Find(wxUniChar ch
, bool fromEnd
= false) const;
482 const int Find(const wxString
& sub
) const;
488 This is a wxWidgets 1.xx compatibility function; you should not use it in new
491 int First(wxChar c
) const;
492 int First(const wxChar
* psz
) const;
493 const int First(const wxString
& str
) const;
497 This static function returns the string containing the result of calling
498 Printf() with the passed parameters on it.
500 @see FormatV(), Printf()
502 static wxString
Format(const wxChar format
, ...);
505 This static function returns the string containing the result of calling
506 PrintfV() with the passed parameters on it.
508 @see Format(), PrintfV()
510 static wxString
FormatV(const wxChar format
, va_list argptr
);
513 Returns the number of occurrences of @a ch in the string.
514 This is a wxWidgets 1.xx compatibility function; you should not use it in new
517 int Freq(wxChar ch
) const;
521 Converts given buffer of binary data from 8-bit string to wxString. In Unicode
522 build, the string is interpreted as being in ISO-8859-1 encoding. The version
523 without @a len parameter takes NUL-terminated data.
524 This is a convenience method useful when storing binary data in wxString.
528 @see wxString::To8BitData
530 static wxString
From8BitData(const char* buf
, size_t len
);
531 static wxString
From8BitData(const char* buf
);
536 Converts the string or character from an ASCII, 7-bit form
537 to the native wxString representation. Most useful when using
538 a Unicode build of wxWidgets (note the use of @c char instead of @c wxChar).
539 Use @ref construct() "wxString constructors" if you
540 need to convert from another charset.
542 static wxString
FromAscii(const char* s
);
543 static wxString
FromAscii(const unsigned char* s
);
544 static wxString
FromAscii(const char* s
, size_t len
);
545 static wxString
FromAscii(const unsigned char* s
, size_t len
);
546 static wxString
FromAscii(char c
);
551 Converts C string encoded in UTF-8 to wxString.
552 Note that this method assumes that @a s is a valid UTF-8 sequence and
553 doesn't do any validation in release builds, it's validity is only checked in
556 static wxString
FromUTF8(const char* s
);
557 static wxString
FromUTF8(const char* s
, size_t len
);
561 Returns the character at position @a n (read-only).
563 wxChar
GetChar(size_t n
) const;
566 wxWidgets compatibility conversion. Returns a constant pointer to the data in
569 const wxChar
* GetData() const;
572 Returns a reference to the character at position @e n.
574 wxChar
GetWritableChar(size_t n
);
577 Returns a writable buffer of at least @a len bytes.
578 It returns a pointer to a new memory block, and the
579 existing data will not be copied.
580 Call UngetWriteBuf() as soon as
581 possible to put the string back into a reasonable state.
582 This method is deprecated, please use
584 wxStringBufferLength instead.
586 wxChar
* GetWriteBuf(size_t len
);
591 This is a wxWidgets 1.xx compatibility function; you should not use it in new
594 size_t Index(wxChar ch
) const;
595 const size_t Index(const wxChar
* sz
) const;
599 Returns @true if the string contains only ASCII characters.
600 This is a wxWidgets 1.xx compatibility function; you should not use it in new
603 bool IsAscii() const;
606 Returns @true if the string is empty.
608 bool IsEmpty() const;
611 Returns @true if the string is empty (same as wxString::IsEmpty).
612 This is a wxWidgets 1.xx compatibility function; you should not use it in new
618 Returns @true if the string is an integer (with possible sign).
619 This is a wxWidgets 1.xx compatibility function; you should not use it in new
622 bool IsNumber() const;
626 Test whether the string is equal to the single character @e c. The test is
627 case-sensitive if @a caseSensitive is @true (default) or not if it is @c
629 Returns @true if the string is equal to the character, @false otherwise.
630 See also Cmp(), CmpNoCase()
632 bool IsSameAs(const wxChar
* psz
, bool caseSensitive
= true) const;
633 const bool IsSameAs(wxChar c
, bool caseSensitive
= true) const;
637 Returns @true if the string is a word.
638 This is a wxWidgets 1.xx compatibility function; you should not use it in new
645 Returns a reference to the last character (writable).
646 This is a wxWidgets 1.xx compatibility function; you should not use it in new
654 Returns the first @a count characters of the string.
656 wxString
Left(size_t count
) const;
659 Returns the length of the string.
664 Returns the length of the string (same as Len).
665 This is a wxWidgets 1.xx compatibility function; you should not use it in new
668 size_t Length() const;
671 Returns this string converted to the lower case.
673 wxString
Lower() const;
677 This is a wxWidgets 1.xx compatibility function; you should not use it in new
683 Converts all characters to lower case and returns the result.
685 wxString
MakeLower();
688 Converts all characters to upper case and returns the result.
690 wxString
MakeUpper();
693 Returns @true if the string contents matches a mask containing '*' and '?'.
695 bool Matches(const wxString
& mask
) const;
698 These are "advanced" functions and they will be needed quite rarely.
699 Alloc() and Shrink() are only
700 interesting for optimization purposes.
702 and wxStringBufferLength classes may be very
703 useful when working with some external API which requires the caller to provide
716 Returns a substring starting at @e first, with length @e count, or the rest of
717 the string if @a count is the default value.
719 wxString
Mid(size_t first
, size_t count
= wxSTRING_MAXLEN
) const;
722 Other string functions.
732 Adds @a count copies of @a pad to the beginning, or to the end of the string
734 Removes spaces from the left or from the right (default).
736 wxString
Pad(size_t count
, wxChar pad
= ' ',
737 bool fromRight
= true);
740 Prepends @a str to this string, returning a reference to this string.
742 wxString
Prepend(const wxString
& str
);
745 Similar to the standard function @e sprintf(). Returns the number of
746 characters written, or an integer less than zero on error.
747 Note that if @c wxUSE_PRINTF_POS_PARAMS is set to 1, then this function supports
748 Unix98-style positional parameters:
750 @note This function will use a safe version of @e vsprintf() (usually called
751 @e vsnprintf()) whenever available to always allocate the buffer of correct
752 size. Unfortunately, this function is not available on all platforms and the
753 dangerous @e vsprintf() will be used then which may lead to buffer overflows.
755 int Printf(const wxChar
* pszFormat
, ...);
758 Similar to vprintf. Returns the number of characters written, or an integer
762 int PrintfV(const wxChar
* pszFormat
, va_list argPtr
);
766 Removes @a len characters from the string, starting at @e pos.
767 This is a wxWidgets 1.xx compatibility function; you should not use it in new
770 wxString
Remove(size_t pos
);
771 wxString
Remove(size_t pos
, size_t len
);
775 Removes the last character.
777 wxString
RemoveLast();
780 Replace first (or all) occurrences of substring with another one.
781 @e replaceAll: global replace (default), or only the first occurrence.
782 Returns the number of replacements made.
784 size_t Replace(const wxString
& strOld
, const wxString
& strNew
,
785 bool replaceAll
= true);
788 Returns the last @a count characters.
790 wxString
Right(size_t count
) const;
793 These functions replace the standard @e strchr() and @e strstr()
802 Sets the character at position @e n.
804 void SetChar(size_t n
, wxChar ch
);
807 Minimizes the string's memory. This can be useful after a call to
808 Alloc() if too much memory were preallocated.
813 This function can be used to test if the string starts with the specified
814 @e prefix. If it does, the function will return @true and put the rest
815 of the string (i.e. after the prefix) into @a rest string if it is not
816 @NULL. Otherwise, the function returns @false and doesn't modify the
819 bool StartsWith(const wxString
& prefix
, wxString rest
= NULL
) const;
822 These functions return the string length and check whether the string is empty
828 @ref operatornot() operator!
837 Strip characters at the front and/or end. The same as Trim except that it
838 doesn't change this string.
839 This is a wxWidgets 1.xx compatibility function; you should not use it in new
842 wxString
Strip(stripType s
= trailing
) const;
845 Returns the part of the string between the indices @a from and @e to
847 This is a wxWidgets 1.xx compatibility function, use Mid()
848 instead (but note that parameters have different meaning).
850 wxString
SubString(size_t from
, size_t to
) const;
853 These functions allow to extract substring from this string. All of them don't
854 modify the original string and return a new string containing the extracted
858 @ref operatorparenth() operator
880 Converts the string to an 8-bit string in ISO-8859-1 encoding in the form of
881 a wxCharBuffer (Unicode builds only).
882 This is a convenience method useful when storing binary data in wxString.
886 @see wxString::From8BitData
888 const char* To8BitData() const;
889 const const wxCharBuffer
To8BitData() const;
894 Converts the string to an ASCII, 7-bit string in the form of
895 a wxCharBuffer (Unicode builds only) or a C string (ANSI builds).
896 Note that this conversion only works if the string contains only ASCII
897 characters. The @ref mbstr() mb_str method provides more
898 powerful means of converting wxString to C string.
900 const char* ToAscii() const;
901 const const wxCharBuffer
ToAscii() const;
905 Attempts to convert the string to a floating point number. Returns @true on
906 success (the number is stored in the location pointed to by @e val) or @false
907 if the string does not represent such number (the value of @a val is not
908 modified in this case).
910 @see ToLong(), ToULong()
912 bool ToDouble(double val
) const;
915 Attempts to convert the string to a signed integer in base @e base. Returns
916 @true on success in which case the number is stored in the location
917 pointed to by @a val or @false if the string does not represent a
918 valid number in the given base (the value of @a val is not modified
920 The value of @a base must be comprised between 2 and 36, inclusive, or
921 be a special value 0 which means that the usual rules of @c C numbers are
922 applied: if the number starts with @c 0x it is considered to be in base
923 16, if it starts with @c 0 - in base 8 and in base 10 otherwise. Note
924 that you may not want to specify the base 0 if you are parsing the numbers
925 which may have leading zeroes as they can yield unexpected (to the user not
926 familiar with C) results.
928 @see ToDouble(), ToULong()
930 bool ToLong(long val
, int base
= 10) const;
933 This is exactly the same as ToLong() but works with 64
935 Notice that currently it doesn't work (always returns @false) if parsing of 64
936 bit numbers is not supported by the underlying C run-time library. Compilers
937 with C99 support and Microsoft Visual C++ version 7 and higher do support this.
939 @see ToLong(), ToULongLong()
941 bool ToLongLong(wxLongLong_t val
, int base
= 10) const;
944 Attempts to convert the string to an unsigned integer in base @e base.
945 Returns @true on success in which case the number is stored in the
946 location pointed to by @a val or @false if the string does not
947 represent a valid number in the given base (the value of @a val is not
948 modified in this case). Please notice that this function
949 behaves in the same way as the standard @c strtoul() and so it simply
950 converts negative numbers to unsigned representation instead of rejecting them
951 (e.g. -1 is returned as @c ULONG_MAX).
952 See ToLong() for the more detailed
953 description of the @a base parameter.
955 @see ToDouble(), ToLong()
957 bool ToULong(unsigned long val
, int base
= 10) const;
960 This is exactly the same as ToULong() but works with 64
962 Please see ToLongLong() for additional remarks.
964 bool ToULongLong(wxULongLong_t val
, int base
= 10) const;
968 Same as @ref wxString::utf8str utf8_str.
970 const char* ToUTF8() const;
971 const const wxCharBuffer
ToUF8() const;
975 Removes white-space (space, tabs, form feed, newline and carriage return) from
976 the left or from the right end of the string (right is default).
978 wxString
Trim(bool fromRight
= true);
981 Truncate the string to the given length.
983 wxString
Truncate(size_t len
);
987 Puts the string back into a reasonable state (in which it can be used
989 GetWriteBuf() was called.
990 The version of the function without the @a len parameter will calculate the
991 new string length itself assuming that the string is terminated by the first
992 @c NUL character in it while the second one will use the specified length
993 and thus is the only version which should be used with the strings with
994 embedded @c NULs (it is also slightly more efficient as @c strlen()
995 doesn't have to be called).
996 This method is deprecated, please use
998 wxStringBufferLength instead.
1000 void UngetWriteBuf();
1001 void UngetWriteBuf(size_t len
);
1005 Returns this string converted to upper case.
1007 wxString
Upper() const;
1010 The same as MakeUpper.
1011 This is a wxWidgets 1.xx compatibility function; you should not use it in new
1017 Both formatted versions (wxString::Printf) and stream-like
1018 insertion operators exist (for basic types only). Additionally, the
1019 Format() function allows to use simply append
1020 formatted value to a string:
1030 @ref operatorout() "operator "
1035 Returns a pointer to the string data (@c const char* in ANSI build,
1036 @c const wchar_t* in Unicode build).
1037 Note that the returned value is not convertible to @c char* or
1038 @c wchar_t*, use @ref charstr() char_str or
1039 @ref wcharstr() wchar_string if you need to pass string value
1040 to a function expecting non-const pointer.
1042 @see @ref mbstr() mb_str, @ref wcstr() wc_str, @ref
1043 fnstr() fn_str, @ref charstr() char_str, @ref
1044 wcharstr() wchar_string
1046 const wxChar
* c_str() const;
1049 Returns an object with string data that is implicitly convertible to
1050 @c char* pointer. Note that any change to the returned buffer is lost and so
1051 this function is only usable for passing strings to legacy libraries that
1052 don't have const-correct API. Use wxStringBuffer if
1053 you want to modify the string.
1055 @see @ref mbstr() mb_str, @ref wcstr() wc_str, @ref
1056 fnstr() fn_str, @ref cstr() c_str, @ref
1057 wcharstr() wchar_str
1059 wxWritableCharBuffer
char_str(const wxMBConv
& conv
= wxConvLibc
) const;
1063 Returns string representation suitable for passing to OS' functions for
1064 file handling. In ANSI build, this is same as @ref cstr() c_str.
1065 In Unicode build, returned value can be either wide character string
1066 or C string in charset matching the @c wxConvFileName object, depending on
1069 @see wxMBConv, @ref wcstr() wc_str, @ref wcstr() mb_str
1071 const wchar_t* fn_str() const;
1072 const const char* fn_str() const;
1073 const const wxCharBuffer
fn_str() const;
1078 Returns multibyte (C string) representation of the string.
1079 In Unicode build, converts using @e conv's wxMBConv::cWC2MB
1080 method and returns wxCharBuffer. In ANSI build, this function is same
1081 as @ref cstr() c_str.
1082 The macro wxWX2MBbuf is defined as the correct return type (without const).
1084 @see wxMBConv, @ref cstr() c_str, @ref wcstr() wc_str, @ref
1085 fnstr() fn_str, @ref charstr() char_str
1087 const char* mb_str(const wxMBConv
& conv
= wxConvLibc
) const;
1088 const const wxCharBuffer
mb_str(const wxMBConv
& conv
= wxConvLibc
) const;
1092 Extraction from a stream.
1094 friend istream
operator(istream
& is
, wxString
& str
);
1098 These functions work as C++ stream insertion operators: they insert the given
1099 value into the string. Precision or format cannot be set using them, you can
1103 wxString
operator(const wxString
& str
);
1104 wxString
operator(const wxChar
* psz
);
1105 wxString
operator(wxChar ch
);
1106 wxString
operator(int i
);
1107 wxString
operator(float f
);
1108 wxString
operator(double d
);
1112 Same as Mid (substring extraction).
1114 wxString
operator ()(size_t start
, size_t len
);
1118 Concatenation: all these operators return a new string equal to the
1119 concatenation of the operands.
1121 wxString
operator +(const wxString
& x
, const wxString
& y
);
1122 wxString
operator +(const wxString
& x
, const wxChar
* y
);
1123 wxString
operator +(const wxString
& x
, wxChar y
);
1124 wxString
operator +(const wxChar
* x
, const wxString
& y
);
1129 Concatenation in place: the argument is appended to the string.
1131 void operator +=(const wxString
& str
);
1132 void operator +=(const wxChar
* psz
);
1133 void operator +=(wxChar c
);
1138 Assignment: the effect of each operation is the same as for the corresponding
1139 constructor (see @ref construct() "wxString constructors").
1141 wxString
operator =(const wxString
& str
);
1142 wxString
operator =(const wxChar
* psz
);
1143 wxString
operator =(wxChar c
);
1150 wxChar
operator [](size_t i
) const;
1151 wxChar
operator [](size_t i
) const;
1152 const wxChar
operator [](int i
) const;
1153 wxChar
operator [](int i
) const;
1157 Implicit conversion to a C string.
1159 operator const wxChar
*() const;
1162 Empty string is @false, so !string will only return @true if the string is
1164 This allows the tests for @NULLness of a @e const wxChar * pointer and emptiness
1165 of the string to look the same in the code and makes it easier to port old code
1169 bool operator!() const;
1172 The supported functions are only listed here, please see any STL reference for
1173 their documentation.
1179 Converts the strings contents to UTF-8 and returns it either as a temporary
1180 wxCharBuffer object or as a pointer to the internal string contents in
1183 const char* utf8_str() const;
1184 const const wxCharBuffer
utf8_str() const;
1189 Returns wide character representation of the string.
1190 In ANSI build, converts using @e conv's wxMBConv::cMB2WC
1191 method and returns wxWCharBuffer. In Unicode build, this function is same
1192 as @ref cstr() c_str.
1193 The macro wxWX2WCbuf is defined as the correct return type (without const).
1195 @see wxMBConv, @ref cstr() c_str, @ref wcstr() mb_str, @ref
1196 fnstr() fn_str, @ref wcharstr() wchar_str
1198 const wchar_t* wc_str(const wxMBConv
& conv
) const;
1199 const const wxWCharBuffer
wc_str(const wxMBConv
& conv
) const;
1203 Returns an object with string data that is implicitly convertible to
1204 @c char* pointer. Note that changes to the returned buffer may or may
1205 not be lost (depending on the build) and so this function is only usable for
1206 passing strings to legacy libraries that don't have const-correct API. Use
1207 wxStringBuffer if you want to modify the string.
1209 @see @ref mbstr() mb_str, @ref wcstr() wc_str, @ref
1210 fnstr() fn_str, @ref cstr() c_str, @ref
1213 wxWritableWCharBuffer
wchar_str() const;
1216 These functions are deprecated, please consider using new wxWidgets 2.0
1217 functions instead of them (or, even better, std::string compatible variants).
1262 wxString wxEmptyString
;
1268 @class wxStringBufferLength
1271 This tiny class allows to conveniently access the wxString
1272 internal buffer as a writable pointer without any risk of forgetting to restore
1273 the string to the usable state later, and allows the user to set the internal
1274 length of the string.
1276 For example, assuming you have a low-level OS function called
1277 @c int GetMeaningOfLifeAsString(char *) copying the value in the provided
1278 buffer (which must be writable, of course), and returning the actual length
1279 of the string, you might call it like this:
1283 wxStringBuffer theAnswerBuffer(theAnswer, 1024);
1284 int nLength = GetMeaningOfLifeAsString(theAnswerBuffer);
1285 theAnswerBuffer.SetLength(nLength);
1286 if ( theAnswer != "42" )
1288 wxLogError("Something is very wrong!");
1292 Note that the exact usage of this depends on whether on not wxUSE_STL is
1294 wxUSE_STL is enabled, wxStringBuffer creates a separate empty character buffer,
1296 if wxUSE_STL is disabled, it uses GetWriteBuf() from wxString, keeping the same
1298 wxString uses intact. In other words, relying on wxStringBuffer containing the
1300 wxString data is probably not a good idea if you want to build your program in
1302 with and without wxUSE_STL.
1304 Note that SetLength @c must be called before wxStringBufferLength destructs.
1309 class wxStringBufferLength
1313 Constructs a writable string buffer object associated with the given string
1314 and containing enough space for at least @a len characters. Basically, this
1315 is equivalent to calling wxString::GetWriteBuf and
1318 wxStringBufferLength(const wxString
& str
, size_t len
);
1321 Restores the string passed to the constructor to the usable state by calling
1322 wxString::UngetWriteBuf on it.
1324 ~wxStringBufferLength();
1327 Sets the internal length of the string referred to by wxStringBufferLength to
1328 @a nLength characters.
1329 Must be called before wxStringBufferLength destructs.
1331 void SetLength(size_t nLength
);
1334 Returns the writable pointer to a buffer of the size at least equal to the
1335 length specified in the constructor.
1337 wxChar
* operator wxChar
*();