C++ Strings

C++ Strings
Constructors
Syntax:
string( size_type length, char ch );
string( const char *str );
string( input_iterator start, input_iterator end );
append
Syntax:
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 );
 
size
Syntax:
size_type size();
The size() function returns the number of characters currently in the string.
at
Syntax:
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'.
begin
Syntax:
iterator begin();
The begin() function returns an iterator to the first element of the current string.
erase
Syntax:
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:
· removes the character pointed to by pos, returning an iterator to the next character,
· removes the characters between start and end, returning an iterator to the character after the last character removed,
· or removes num characters from the current string, starting at index, and returns *this.
The parameters index and num have default values, which means that erase() can be called with just index to erase all characters after index or with no arguments to erase all characters. For example:
 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 ''
find
Syntax:
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:
returns the first occurrence of str within the current string, starting at index, string::npos if nothing is found, 
returns the first occurrence of str within the current string and within length characters, starting at index, string::npos if nothing is found, 
or returns the index of the first occurrence ch within the current string, starting at index, string::npos if nothing is found. 
For example, 
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;
insert
Syntax:
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 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:
inserts ch before the character denoted by i, 
inserts str into the current string, at location index, 
inserts a substring of str (starting at index2 and num characters long) into the current string, at location index1, 
inserts num characters of str into the current string, at location index, 
inserts num copies of ch into the current string, at location index, 
inserts num copies of ch into the current string, before the character denoted by i, 
or inserts the characters denoted by start and end into the current string, before the character specified by i. 

你可能感兴趣的:(String)