C++ 字符串实用参考
2011-09-08 21:37 netwy 阅读(130) 评论(0) 收藏 举报一、C 风格字符串,用来处理单字节字符串
cstring (string.h)
header
C Strings
This header file defines several functions to manipulate C strings and arrays.
Functions
Copying:
| memcpy | Copy block of memory (function) |
| memmove | Move block of memory (function ) |
| strcpy | Copy string (function) |
| strncpy | Copy characters from string (function) |
Concatenation:
| strcat | Concatenate strings (function) |
| strncat | Append characters from string (function) |
Comparison:
| memcmp | Compare two blocks of memory (function) |
| strcmp | Compare two strings (function ) |
| strcoll | Compare two strings using locale (function) |
| strncmp | Compare characters of two strings (function) |
| strxfrm | Transform string using locale (function) |
Searching:
| memchr | Locate character in block of memory (function) |
| strchr | Locate first occurrence of character in string (function) |
| strcspn | Get span until character in string (function) |
| strpbrk | Locate character in string (function) |
| strrchr | Locate last occurrence of character in string (function) |
| strspn | Get span of character set in string (function) |
| strstr | Locate substring (function) |
| strtok | Split string into tokens (function) |
Other:
| memset | Fill block of memory (function) |
| strerror | Get pointer to error message string (function) |
| strlen | Get string length (function ) |
Macros
| NULL | Null pointer (macro) |
Types
| size_t | Unsigned integral type (type) |
二、stl::string 标准库提供的字符串类
String class
String objects are a special type of container, specifically designed to operate with sequences of characters.
Unlike traditional c-strings, which are mere sequences of characters in a memory array, C++ string objects belong to a class with many built-in features to operate with strings in a more intuitive way and with some additional useful features common to C++ containers.
The string class is an instantiation of the basic_string class template, defined in <string> as:
Unlike traditional c-strings, which are mere sequences of characters in a memory array, C++ string objects belong to a class with many built-in features to operate with strings in a more intuitive way and with some additional useful features common to C++ containers.
The string class is an instantiation of the basic_string class template, defined in <string> as:
typedef basic_string<char> string; typedef basic_string<wchar_t> wstring;
Member functions
| (constructor) | Construct string object (constructor member ) |
| operator= | String assignment (public member function) |
Iterators:
| begin | Return iterator to beginning (public member function) |
| end | Return iterator to end (public member function) |
| rbegin | Return reverse iterator to reverse beginning (public member function) |
| rend | Return reverse iterator to reverse end (public member function) |
Capacity:
| size | Return length of string (public member function) |
| length | Return length of string (public member function) |
| max_size | Return maximum size of string (public member function) |
| resize | Resize string (public member function) |
| capacity | Return size of allocated storage (public member function) |
| reserve | Request a change in capacity (public member function) |
| clear | Clear string (public member function) |
| empty | Test if string is empty (public member function) |
Element access:
| operator[] | Get character in string (public member function) |
| at | Get character in string (public member function) |
Modifiers:
| operator+= | Append to string (public member function) |
| append | Append to string (public member function) |
| push_back | Append character to string (public member function) |
| assign | Assign content to string (public member function ) |
| insert | Insert into string (public member function ) |
| erase | Erase characters from string (public member function) |
| replace | Replace part of string (public member function ) |
| swap | Swap contents with another string (public member function) |
String operations:
| c_str | Get C string equivalent (public member function ) |
| data | Get string data (public member function) |
| get_allocator | Get allocator (public member function) |
| copy | Copy sequence of characters from string (public member function) |
| find | Find content in string (public member function) |
| rfind | Find last occurrence of content in string (public member function) |
| find_first_of | Find character in string (public member function) |
| find_last_of | Find character in string from the end (public member function) |
| find_first_not_of | Find absence of character in string |
| find_last_not_of | Find absence of character in string from the end (public member function) |
| substr | Generate substring (public member function) |
| compare | Compare strings (public member function ) |
三、String-Manipulation Routines
String-Manipulation Routines
| Routine | Use |
|---|---|
| _mbscoll, _mbsicoll, _mbsncoll, _mbsnicoll | Compare two multibyte-character strings using multibyte code page information (_mbsicoll and _mbsnicoll are case-insensitive) |
| _mbsdec, _strdec, _wcsdec | Move string pointer back one character |
| _mbsinc, _strinc, _wcsinc | Advance string pointer by one character |
| _mbslen | Get number of multibyte characters in multibyte-character string; dependent upon OEM code page |
| _mbsnbcat | Append, at most, first n bytes of one multibyte-character string to another |
| _mbsnbcmp | Compare first n bytes of two multibyte-character strings |
| _mbsnbcnt | Return number of multibyte-character bytes within supplied character count |
| _mbsnbcpy | Copy n bytes of string |
| _mbsnbicmp | Compare n bytes of two multibyte-character strings, ignoring case |
| _mbsnbset | Set first n bytes of multibyte-character string to specified character |
| _mbsnccnt | Return number of multibyte characters within supplied byte count |
| _mbsnextc, _strnextc, _wcsnextc | Find next character in string |
| _mbsninc. _strninc, _wcsninc | Advance string pointer by n characters |
| _mbsspnp, _strspnp, _wcsspnp | Return pointer to first character in given string that is not in another given string |
| _mbstrlen | Get number of multibyte characters in multibyte-character string; locale-dependent |
| _scprintf, _scwprintf | Return the number of characters in a formatted string |
| _snscanf, _snwscanf | Read formatted data of a specified length from the standard input stream. |
| sprintf, _stprintf | Write formatted data to a string |
| strcat, wcscat, _mbscat | Append one string to another |
| strchr, wcschr, _mbschr | Find first occurrence of specified character in string |
| strcmp, wcscmp, _mbscmp | Compare two strings |
| strcoll, wcscoll, _stricoll, _wcsicoll, _strncoll, _wcsncoll, _strnicoll, _wcsnicoll | Compare two strings using current locale code page information (_stricoll, _wcsicoll, _strnicoll, and _wcsnicoll are case-insensitive) |
| strcpy, wcscpy, _mbscpy | Copy one string to another |
| strcspn, wcscspn, _mbscspn, | Find first occurrence of character from specified character set in string |
| _strdup, _wcsdup, _mbsdup | Duplicate string |
| strerror, _wcserror | Map error number to message string |
| _strerror, __wcserror | Map user-defined error message to string |
| strftime, wcsftime | Format date-and-time string |
| _stricmp, _wcsicmp, _mbsicmp | Compare two strings without regard to case |
| strlen, wcslen, _mbslen, _mbstrlen | Find length of string |
| _strlwr, _wcslwr, _mbslwr | Convert string to lowercase |
| strncat, wcsncat, _mbsncat | Append characters of string |
| strncmp, wcsncmp, _mbsncmp | Compare characters of two strings |
| strncpy, wcsncpy, _mbsncpy | Copy characters of one string to another |
| _strnicmp, _wcsnicmp, _mbsnicmp | Compare characters of two strings without regard to case |
| _strnset, _wcsnset, _mbsnset | Set first n characters of string to specified character |
| strpbrk, wcspbrk, _mbspbrk | Find first occurrence of character from one string in another string |
| strrchr, wcsrchr,_mbsrchr | Find last occurrence of given character in string |
| _strrev, _wcsrev,_mbsrev | Reverse string |
| _strset, _wcsset, _mbsset | Set all characters of string to specified character |
| strspn, wcsspn, _mbsspn | Find first substring from one string in another string |
| strstr, wcsstr, _mbsstr | Find first occurrence of specified string in another string |
| strtok, wcstok, _mbstok | Find next token in string |
| _strupr, _wcsupr, _mbsupr | Convert string to uppercase |
| strxfrm, wcsxfrm | Transform string into collated form based on locale-specific information |
| vsprintf, _vstprint | Write formatted output using a pointer to a list of arguments |
浙公网安备 33010602011771号