📄 wxstring.tex
字号:
\section{\class{wxString}}\label{wxstring}wxString is a class representing a character string. Please see the \helpref{wxString overview}{wxstringoverview} for more information about it.As explained there, wxString implements most of the methods of the std::stringclass.These standard functions are not documented in this manual, please see the\urlref{STL documentation}{http://www.cppreference.com/cppstl.html}).The behaviour of all these functions is identical to the behaviour describedthere.You may notice that wxString sometimes has many functions which do the samething like, for example, \helpref{Length()}{wxstringlength}, \helpref{Len()}{wxstringlen} and {\tt length()} which all return the stringlength. In all cases of such duplication the {\tt std::string}-compatiblemethod ({\tt length()} in this case, always the lowercase version) should beused as it will ensure smoother transition to {\tt std::string} when wxWidgetsstarts using it instead of wxString.\wxheading{Derived from}None\wxheading{Include files}<wx/string.h>\wxheading{Predefined objects}Objects:{\bf wxEmptyString}\wxheading{See also}\helpref{wxString overview}{wxstringoverview}, \helpref{Unicode overview}{unicode}\latexignore{\rtfignore{\wxheading{Function groups}}}\membersection{Constructors and assignment operators}\label{constructorsinwxstring}A string may be constructed either from a C string, (some number of copies of)a single character or a wide (UNICODE) string. For all constructors (except thedefault which creates an empty string) there is also a corresponding assignmentoperator.\helpref{wxString}{wxstringconstruct}\\\helpref{operator $=$}{wxstringoperatorassign}\\\helpref{\destruct{wxString}}{wxstringdestruct}\membersection{String length}\label{lengthfunctionsinwxstring}These functions return the string length and check whether the string is emptyor empty it.\helpref{Len}{wxstringlen}\\\helpref{IsEmpty}{wxstringisempty}\\\helpref{operator!}{wxstringoperatornot}\\\helpref{Empty}{wxstringempty}\\\helpref{Clear}{wxstringclear}\membersection{Character access}\label{characteraccessinwxstring}Many functions in this section take a character index in the string. As with Cstrings and/or arrays, the indices start from $0$, so the first character of astring is string[$0$]. Attempt to access a character beyond the end of thestring (which may be even $0$ if the string is empty) will provoke an assertfailure in \helpref{debug build}{debuggingoverview}, but no checks are done inrelease builds.This section also contains both implicit and explicit conversions to C stylestrings. Although implicit conversion is quite convenient, it is advised to useexplicit \helpref{c\_str()}{wxstringcstr} method for the sake of clarity. Alsosee \helpref{overview}{wxstringadvices} for the cases where it is necessary touse it.\helpref{GetChar}{wxstringgetchar}\\\helpref{GetWritableChar}{wxstringgetwritablechar}\\\helpref{SetChar}{wxstringsetchar}\\\helpref{Last}{wxstringlast}\\\helpref{operator []}{wxstringoperatorbracket}\\\helpref{c\_str}{wxstringcstr}\\\helpref{mb\_str}{wxstringmbstr}\\\helpref{wc\_str}{wxstringwcstr}\\\helpref{fn\_str}{wxstringfnstr}\\\helpref{operator const char*}{wxstringoperatorconstcharpt}\membersection{Concatenation}\label{concatenationinwxstring}Anything may be concatenated (appended to) with a string. However, you can'tappend something to a C string (including literal constants), so to do this itshould be converted to a wxString first.\helpref{operator \cinsert}{wxstringoperatorout}\\\helpref{operator $+=$}{wxstringplusequal}\\\helpref{operator $+$}{wxstringoperatorplus}\\\helpref{Append}{wxstringappend}\\\helpref{Prepend}{wxstringprepend}\membersection{Comparison}\label{comparisoninwxstring}The default comparison function \helpref{Cmp}{wxstringcmp} is case-sensitive andso is the default version of \helpref{IsSameAs}{wxstringissameas}. For caseinsensitive comparisons you should use \helpref{CmpNoCase}{wxstringcmpnocase} orgive a second parameter to IsSameAs. This last function is may be moreconvenient if only equality of the strings matters because it returns a boolean\true value if the strings are the same and not 0 (which is usually false in C)as {\tt Cmp()} does.\helpref{Matches}{wxstringmatches} is a poor man's regular expression matcher:it only understands '*' and '?' metacharacters in the sense of DOS command lineinterpreter.\helpref{StartsWith}{wxstringstartswith} is helpful when parsing a line oftext which should start with some predefined prefix and is more efficient thandoing direct string comparison as you would also have to precalculate thelength of the prefix then.\helpref{Cmp}{wxstringcmp}\\\helpref{CmpNoCase}{wxstringcmpnocase}\\\helpref{IsSameAs}{wxstringissameas}\\\helpref{Matches}{wxstringmatches}\\\helpref{StartsWith}{wxstringstartswith}\\\helpref{EndsWith}{wxstringendswith}\membersection{Substring extraction}\label{substringextractioninwxstring}These functions allow to extract substring from this string. All of them don'tmodify the original string and return a new string containing the extractedsubstring.\helpref{Mid}{wxstringmid}\\\helpref{operator()}{wxstringoperatorparenth}\\\helpref{Left}{wxstringleft}\\\helpref{Right}{wxstringright}\\\helpref{BeforeFirst}{wxstringbeforefirst}\\\helpref{BeforeLast}{wxstringbeforelast}\\\helpref{AfterFirst}{wxstringafterfirst}\\\helpref{AfterLast}{wxstringafterlast}\\\helpref{StartsWith}{wxstringstartswith}\\\helpref{EndsWith}{wxstringendswith}\membersection{Case conversion}\label{caseconversioninwxstring}The MakeXXX() variants modify the string in place, while the other functionsreturn a new string which contains the original text converted to the upper orlower case and leave the original string unchanged.\helpref{MakeUpper}{wxstringmakeupper}\\\helpref{Upper}{wxstringupper}\\\helpref{MakeLower}{wxstringmakelower}\\\helpref{Lower}{wxstringlower}\membersection{Searching and replacing}\label{searchingandreplacinginwxstring}These functions replace the standard {\it strchr()} and {\it strstr()} functions.\helpref{Find}{wxstringfind}\\\helpref{Replace}{wxstringreplace}\membersection{Conversion to numbers}\label{conversiontonumbersinwxstring}The string provides functions for conversion to signed and unsigned integer andfloating point numbers. All three functions take a pointer to the variable toput the numeric value in and return \true if the {\bf entire} string could beconverted to a number.\helpref{ToLong}{wxstringtolong}\\\helpref{ToLongLong}{wxstringtolonglong}\\\helpref{ToULong}{wxstringtoulong}\\\helpref{ToULongLong}{wxstringtoulonglong}\\\helpref{ToDouble}{wxstringtodouble}\membersection{Writing values into the string}\label{writingintostringinwxstring}Both formatted versions (\helpref{Printf}{wxstringprintf}) and stream-likeinsertion operators exist (for basic types only). Additionally, the \helpref{Format}{wxstringformat} function allows to use simply appendformatted value to a string:\begin{verbatim} // the following 2 snippets are equivalent wxString s = "..."; s += wxString::Format("%d", n); wxString s; s.Printf("...%d", n);\end{verbatim}\helpref{Format}{wxstringformat}\\\helpref{FormatV}{wxstringformatv}\\\helpref{Printf}{wxstringprintf}\\\helpref{PrintfV}{wxstringprintfv}\\\helpref{operator \cinsert}{wxstringoperatorout}\membersection{Memory management}\label{memoryinwxstring}These are "advanced" functions and they will be needed quite rarely. \helpref{Alloc}{wxstringalloc} and \helpref{Shrink}{wxstringshrink} are onlyinteresting for optimization purposes. \helpref{GetWriteBuf}{wxstringgetwritebuf} may be very useful when working withsome external API which requires the caller to provide a writable buffer, butextreme care should be taken when using it: before performing any otheroperation on the string \helpref{UngetWriteBuf}{wxstringungetwritebuf} {\bfmust} be called!\helpref{Alloc}{wxstringalloc}\\\helpref{Shrink}{wxstringshrink}\\\helpref{GetWriteBuf}{wxstringgetwritebuf}\\\helpref{UngetWriteBuf}{wxstringungetwritebuf}\membersection{Miscellaneous}\label{miscellaneousinwxstring}Other string functions.\helpref{Trim}{wxstringtrim}\\\helpref{Truncate}{wxstringtruncate}\\\helpref{Pad}{wxstringpad}\membersection{wxWidgets 1.xx compatibility functions}\label{backwardcompatibilityinwxstring}These functions are deprecated, please consider using new wxWidgets 2.0functions instead of them (or, even better, std::string compatible variants).% keep ordered alphabetically\helpref{CompareTo}{wxstringcompareto}\\\helpref{Contains}{wxstringcontains}\\\helpref{First}{wxstringfirst}\\\helpref{Freq}{wxstringfreq}\\\helpref{Index}{wxstringindex}\\\helpref{IsAscii}{wxstringisascii}\\\helpref{IsNull}{wxstringisnull}\\\helpref{IsNumber}{wxstringisnumber}\\\helpref{IsWord}{wxstringisword}\\\helpref{Last}{wxstringlast}\\\helpref{Length}{wxstringlength}\\\helpref{LowerCase}{wxstringlowercase}\\\helpref{Remove}{wxstringremove}\\\helpref{Strip}{wxstringstrip}\\\helpref{SubString}{wxstringsubstring}\\\helpref{UpperCase}{wxstringuppercase}\membersection{std::string compatibility functions}\label{wxstringat}The supported functions are only listed here, please see any STL reference fortheir documentation.\begin{verbatim} // take nLen chars starting at nPos wxString(const wxString& str, size_t nPos, size_t nLen); // take all characters from pStart to pEnd (poor man's iterators) wxString(const void *pStart, const void *pEnd); // lib.string.capacity // return the length of the string size_t size() const; // return the length of the string size_t length() const; // return the maximum size of the string size_t max_size() const; // resize the string, filling the space with c if c != 0 void resize(size_t nSize, char ch = '\0'); // delete the contents of the string void clear(); // returns true if the string is empty bool empty() const; // lib.string.access // return the character at position n char at(size_t n) const; // returns the writable character at position n char& at(size_t n); // lib.string.modifiers // append a string wxString& append(const wxString& str); // append elements str[pos], ..., str[pos+n] wxString& append(const wxString& str, size_t pos, size_t n); // append first n (or all if n == npos) characters of sz wxString& append(const char *sz, size_t n = npos); // append n copies of ch wxString& append(size_t n, char ch); // same as `this_string = str' wxString& assign(const wxString& str); // same as ` = str[pos..pos + n] wxString& assign(const wxString& str, size_t pos, size_t n); // same as `= first n (or all if n == npos) characters of sz' wxString& assign(const char *sz, size_t n = npos); // same as `= n copies of ch' wxString& assign(size_t n, char ch); // insert another string wxString& insert(size_t nPos, const wxString& str); // insert n chars of str starting at nStart (in str) wxString& insert(size_t nPos, const wxString& str, size_t nStart, size_t n); // insert first n (or all if n == npos) characters of sz wxString& insert(size_t nPos, const char *sz, size_t n = npos); // insert n copies of ch wxString& insert(size_t nPos, size_t n, char ch); // delete characters from nStart to nStart + nLen wxString& erase(size_t nStart = 0, size_t nLen = npos); // replaces the substring of length nLen starting at nStart wxString& replace(size_t nStart, size_t nLen, const char* sz); // replaces the substring with nCount copies of ch wxString& replace(size_t nStart, size_t nLen, size_t nCount, char ch); // replaces a substring with another substring wxString& replace(size_t nStart, size_t nLen, const wxString& str, size_t nStart2, size_t nLen2); // replaces the substring with first nCount chars of sz wxString& replace(size_t nStart, size_t nLen, const char* sz, size_t nCount); // swap two strings void swap(wxString& str); // All find() functions take the nStart argument which specifies the // position to start the search on, the default value is 0. All functions // return npos if there were no match. // find a substring size_t find(const wxString& str, size_t nStart = 0) const; // find first n characters of sz size_t find(const char* sz, size_t nStart = 0, size_t n = npos) const; // find the first occurrence of character ch after nStart size_t find(char ch, size_t nStart = 0) const; // rfind() family is exactly like find() but works right to left // as find, but from the end size_t rfind(const wxString& str, size_t nStart = npos) const; // as find, but from the end size_t rfind(const char* sz, size_t nStart = npos, size_t n = npos) const; // as find, but from the end size_t rfind(char ch, size_t nStart = npos) const; // find first/last occurrence of any character in the set // size_t find_first_of(const wxString& str, size_t nStart = 0) const; // size_t find_first_of(const char* sz, size_t nStart = 0) const; // same as find(char, size_t) size_t find_first_of(char c, size_t nStart = 0) const; // size_t find_last_of (const wxString& str, size_t nStart = npos) const; // size_t find_last_of (const char* s, size_t nStart = npos) const; // same as rfind(char, size_t) size_t find_last_of (char c, size_t nStart = npos) const; // find first/last occurrence of any character not in the set // size_t find_first_not_of(const wxString& str, size_t nStart = 0) const; // size_t find_first_not_of(const char* s, size_t nStart = 0) const; // size_t find_first_not_of(char ch, size_t nStart = 0) const; // size_t find_last_not_of(const wxString& str, size_t nStart=npos) const;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -