cppreference.com -> C++ Strings -> Details

C++ Strings


Constructors

Syntax:

  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:

For example,

    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
    

Operators

Syntax:

  ==
  >
  <
  >=
  <=
  !=
  +
  +=
  []

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:
at(), compare().

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 );
  basic_string &append( size_type num, char ch );
  basic_string &append( input_iterator start, input_iterator end );

The append() function either:

For example, the following code:

    string str = "Hello World";
    str.append( 10, '!' );
    cout << str << endl;

displays

    Hello World!!!!!!!!!!
Related topics:
The + operator

assign

Syntax:

  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:

For example, the following code:

    string str1, str2 = "War and Peace";
    str1.assign( str2, 4, 3 );  
    cout << str1 << endl;

displays

    and
    

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'.

Related topics:
The [] operator

begin

Syntax:

  iterator begin();

The begin() function returns an iterator to the first element of the current string.

Related topics:
end()

c_str

Syntax:

  const char *c_str();

The function c_str() returns a pointer to a regular C string, identical to the current string.

Related topics:
The [] operator

capacity

Syntax:

  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:
max_size(), reserve(), resize(), size(),

compare

Syntax:

  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 ValueCase
less than zerothis < str
zerothis == str
greater than zerothis > str

The various functions either:

Related topics:
Operators

copy

Syntax:

  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.


data

Syntax:

  const char *data();

The function data() returns a pointer to the first character in the current string.

Related topics:
c_str()

empty

Syntax:

  bool empty();

The empty() function returns true if the current string is empty, and false otherwise.


end

Syntax:

  iterator end();

The end() function returns an iterator to the end of the string.

Related topics:
begin()

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:

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:

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;
    
    

find_first_of

Syntax:

  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:

Related topics:
find()

find_first_not_of

Syntax:

  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:

Related topics:
find()

find_last_of

Syntax:

  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:

Related topics:
find()

find_last_not_of

Syntax:

  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:

Related topics:
find()

get_allocator

Syntax:

  allocator_type get_allocator();

The function get_allocator() returns the allocator for the current string.


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 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:

Related topics:
replace()

length

Syntax:

  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()

max_size

Syntax:

  size_type max_size();

The max_size() function returns the maximum number of characters that the string can hold.


rbegin

Syntax:

  const reverse_iterator rbegin();

The function rbegin() returns a reverse iterator to the end of the current string.

Related topics:
rend()

rend

Syntax:

  const reverse_iterator rend();

The rend() function returns a reverse iterator to the start of the current string.

Related topics:
rbegin()

replace

Syntax:

  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:

For example, the following code displays the string "They say he carved it himself...find your soul-mate, Homer."
    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:
insert()

reserve

Syntax:

  void reserve( size_type num );

The function reserve() sets the capacity() of the current string to at least num.

Related topics:
capacity()

resize

Syntax:

  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.


rfind

Syntax:

  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:

For example, in the following code, the first call to rfind() returns string::npos, because the target word is not within the first 8 characters of the string. However, the second call returns 9, because the target word is within 20 characters of the beginning of the string.
    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:
find()

size

Syntax:

  size_type size();

The size() function returns the number of characters currently in the string.

Related topics:
length(), max_size()

substr

Syntax:

  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

swap

Syntax:

  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