Espaços nominais
Variantes
Acções

replace

Da cppreference.com
< cpp‎ | string‎ | basic string
Revisão das 04h18min de 1 de maio de 2012 por P12 (discussão | contribs)

(dif) ← Revisão anterior | Revisão actual (dif) | Revisão seguinte → (dif)

Sintaxe:

    #include <string>
    string& replace( size_type index, size_type num, const string& str );
    string& replace( size_type index1, size_type num1, const string& str, size_type index2, size_type num2 );
    string& replace( size_type index, size_type num, const charT* str );
    string& replace( size_type index, size_type num1, const charT* str, size_type num2 );
    string& replace( size_type index, size_type num1, size_type num2, charT ch);
 
    string& replace( iterator start, iterator end, const string& str );
    string& replace( iterator start, iterator end, const charT* str );
    string& replace( iterator start, iterator end, const charT* str, size_type num );
    string& replace( iterator start, iterator end, size_type num, charT ch );
    string& replace( iterator start, iterator end, input_iterator start2, input_iterator end2 );

A função replace():

  • substitui até num caracteres da string actual com caracteres de str, começando em index,
  • substitui até num1 caracteres da string actual (começando em index1) com no máximo num2 caracteres de str começando em index2,
  • substitui até num caracteres da string actual com caracteres de str, começando na posição index em str,
  • substitui até num1 caracteres da string actual (começando em index1) com num2 caracteres de str começando em index2,
  • substitui até num1 caracteres na string actual (começando em index) com num2 cópias de ch,
  • substitui os caracteres da string actual de start a end com str,
  • substitui caracteres na string actual de start a end com num caracteres de str,
  • ou substitui os caracteres na string actual de start a end com num cópias de ch.

Por exemplo, o código seguinte mostra a 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, s.length() - 32, s2 );
     cout << s << endl;

Tópicos Relacionados: insert