cppreference.com -> C++ Strings -> Details |
string(); string( size_type length, char ch ); string( const char *str ); string( const char *str, size_type length ); string( string &str, size_type index, size_type length ); string( input_iterator start, input_iterator end ); |
The string constructors create a new string containing:
string str1( 5, 'c' ); string str2( "Now is the time..." ); string str3( str2, 11, 4 ); cout << str1 << endl; cout << str2 << endl; cout << str3 << endl;
displays
ccccc Now is the time... time
== > < >= <= != + += [] |
You can compare strings with ==, >, <, >=, <=, and !=. You can combine two strings with the + operator or the += operator, and look at a specific character with the [] operator.
Related topics:basic_string &append( const basic_string &str ); basic_string &append( const char *str ); basic_string &append( const basic_string &str, size_type index, size_type len ); basic_string &append( const char *str, size_type num ); basic_string &append( size_type num, char ch ); basic_string &append( input_iterator start, input_iterator end ); |
The append() function either:
string str = "Hello World"; str.append( 10, '!' ); cout << str << endl;
displays
Hello World!!!!!!!!!!Related topics:
basic_string &assign( const basic_string &str ); basic_string &assign( const char *str ); basic_string &assign( const char *str, size_type num ); basic_string &assign( const basic_string &str, size_type index, size_type len ); basic_string &assign( size_type num, char ch ); |
The function assign either:
string str1, str2 = "War and Peace"; str1.assign( str2, 4, 3 ); cout << str1 << endl;
displays
and
reference at( size_type index ); |
The at() function returns a reference to the character at location index. If index is not within the string, then at() reports an out of range error by throwing an object of the out_of_range class. For example, this code:
string text = "ABCDEF"; char ch = text.at( 2 );
displays the character 'C'.
Related topics:iterator begin(); |
The begin() function returns an iterator to the first element of the current string.
Related topics:const char *c_str(); |
The function c_str() returns a pointer to a regular C string, identical to the current string.
Related topics:size_type capacity(); |
The capacity() function returns the number of characters that the current string can hold before it will need to allocate more memory. This number will be at least as large as size().
Related topics:int compare( const basic_string &str ); int compare( const char *str ); int compare( size_type index, size_type length, const basic_string &str ); int compare( size_type index, size_type length, const basic_string &str, size_type index2, size_type length2 ); int compare( size_type index, size_type length, const char *str, size_type length2 ); |
The compare() function either compares str to the current string in a variety of ways, returning
Return Value | Case |
---|---|
less than zero | this < str |
zero | this == str |
greater than zero | this > str |
The various functions either:
size_type copy( char *str, size_type num, size_type index ); |
The copy() function copies num characters of the current string (starting at index) into str. The return value is the number of characters copied.
const char *data(); |
The function data() returns a pointer to the first character in the current string.
Related topics:bool empty(); |
The empty() function returns true if the current string is empty, and false otherwise.
iterator end(); |
The end() function returns an iterator to the end of the string.
Related topics:iterator erase( iterator pos ); iterator erase( iterator start, iterator end ); basic_string &erase( size_type index = 0, size_type num = npos ); |
The erase() function either:
string s("So, you like donuts, eh? Well, have all the donuts in the world!"); cout << "The original string is '" << s << "'" << endl; s.erase( 50, 14 ); cout << "Now the string is '" << s << "'" << endl; s.erase( 24 ); cout << "Now the string is '" << s << "'" << endl; s.erase(); cout << "Now the string is '" << s << "'" << endl;
will display
The original string is 'So, you like donuts, eh? Well, have all the donuts in the world!' Now the string is 'So, you like donuts, eh? Well, have all the donuts' Now the string is 'So, you like donuts, eh?' Now the string is ''
size_type find( const basic_string &str, size_type index ); size_type find( const char *str, size_type index ); size_type find( const char *str, size_type index, size_type length ); size_type find( char ch, size_type index ); |
The function find() either:
string str1( "Alpha Beta Gamma Delta" ); unsigned int loc = str1.find( "Omega", 0 ); if( loc != string::npos ) cout << "Found Omega at " << loc << endl; else cout << "Didn't find Omega" << endl;
size_type find_first_of( const basic_string &str, size_type index ); size_type find_first_of( const char *str, size_type index ); size_type find_first_of( const char *str, size_type index, size_type num ); size_type find_first_of( char ch, size_type index ); |
The find_first_of() function either:
size_type find_first_not_of( const basic_string &str, size_type index ); size_type find_first_not_of( const char *str, size_type index ); size_type find_first_not_of( const char *str, size_type index, size_type num ); size_type find_first_not_of( char ch, size_type index ); |
The find_first_not_of() function either:
size_type find_last_of( const basic_string &str, size_type index ); size_type find_last_of( const char *str, size_type index ); size_type find_last_of( const char *str, size_type index, size_type num ); size_type find_last_of( char ch, size_type index ); |
The find_last_of() function either:
size_type find_last_not_of( const basic_string &str, size_type index ); size_type find_last_not_of( const char *str, size_type index ); size_type find_last_not_of( const char *str, size_type index, size_type num ); size_type find_last_not_of( char ch, size_type index ); |
The find_last_not_of() function either:
allocator_type get_allocator(); |
The function get_allocator() returns the allocator for the current string.
iterator insert( iterator i, const char &ch ); basic_string &insert( size_type index, const basic_string &str ); basic_string &insert( size_type index, const char *str ); basic_string &insert( size_type index1, const basic_string &str, size_type index2, size_type num ); basic_string &insert( size_type index, const char *str, size_type num ); basic_string &insert( size_type index, size_type num, char ch ); void insert( iterator i, size_type num, const char &ch ); void insert( iterator i, iterator start, iterator end ); |
The very multi-purpose insert() function either:
size_type length(); |
The function length() returns the length of the current string. This number should be the same as the one returned from size().
Related topics:size_type max_size(); |
The max_size() function returns the maximum number of characters that the string can hold.
const reverse_iterator rbegin(); |
The function rbegin() returns a reverse iterator to the end of the current string.
Related topics:const reverse_iterator rend(); |
The rend() function returns a reverse iterator to the start of the current string.
Related topics:basic_string &replace( size_type index, size_type num, const basic_string &str ); basic_string &replace( size_type index1, size_type num1, const basic_string &str, size_type index2, size_type num2 ); basic_string &replace( size_type index, size_type num, const char *str ); basic_string &replace( size_type index, size_type num1, const char *str, size_type num2 ); basic_string &replace( size_type index, size_type num1, size_type num2, char ch ); basic_string &replace( iterator start, iterator end, const basic_string &str ); basic_string &replace( iterator start, iterator end, const char *str ); basic_string &replace( iterator start, iterator end, const char *str, size_type num ); basic_string &replace( iterator start, iterator end, size_type num, char ch ); |
The function replace() either:
string s = "They say he carved it himself...from a BIGGER spoon"; string s2 = "find your soul-mate, Homer."; s.replace( 32, s2.length(), s2 ); cout << s << endl;Related topics:
void reserve( size_type num ); |
The function reserve() sets the capacity() of the current string to at least num.
Related topics:void resize( size_type num ); void resize( size_type num, char ch ); |
The resize() function changes the size of the current string to num, optionally padding any new space with copies of ch.
size_type rfind( const basic_string &str, size_type index ); size_type rfind( const char *str, size_type index ); size_type rfind( const char *str, size_type index, size_type num ); size_type rfind( char ch, size_type index ); |
The rfind() function either:
int loc; string s = "My cat's breath smells like cat food."; loc = s.rfind( "breath", 8 ); cout << "The word breath is at index " << loc << endl; loc = s.rfind( "breath", 20 ); cout << "The word breath is at index " << loc << endl;Related topics:
size_type size(); |
The size() function returns the number of characters currently in the string.
Related topics:basic_string substr( size_type index, size_type num = npos ); |
The substr() function returns a substring of the current string, starting at index, and num characters long. If npos is omitted, it will default to string::npos, and the substr() function will simply return the remainder of the string starting at index. For example:
string s("What we have here is a failure to communicate"); string sub = s.substr(21); cout << "The original string is " << s << endl; cout << "The substring is " << sub << endl;
displays
The original string is What we have here is a failure to communicate The substring is a failure to communicate
void swap( basic_string &str ); |
The function swap() switches str with the current string. For example:
string first( "This comes first" ); string second( "And this is second" ); first.swap( second ); cout << first << endl; cout << second << endl;
displays
And this is second This comes first