C++

Topic: String

Explain about replace method with example ?

string& replace ( size_t pos1, size_t n1,   const string& str );string& replace ( iterator i1, iterator i2, const string& str );string& replace ( size_t pos1, size_t n1, const string& str, size_t pos2, size_t n2 );string& replace ( size_t pos1, size_t n1,   const char* s, size_t n2 );string& replace ( iterator i1, iterator i2, const char* s, size_t n2 );string& replace ( size_t pos1, size_t n1,   const char* s );string& replace ( iterator i1, iterator i2, const char* s );string& replace ( size_t pos1, size_t n1,   size_t n2, char c );string& replace ( iterator i1, iterator i2, size_t n2, char c );template<class InputIterator>   string& replace ( iterator i1, iterator i2, InputIterator j1, InputIterator j2 );Replace part of stringReplaces a section of the current string by some other content determined by the arguments passed.For the versions with parameters pos1 and n1, the section replaced begins at character position pos1 and spans for n1 characters within the string.For the versions with iterators i1 and i2, the section replaced is the one formed by the elements between iterator i1 and the element right before iterator i2.The arguments passed to the function determine what is the replacement for this section of the string:string& replace ( size_t pos1, size_t n1, const string& str );string& replace ( iterator i1, iterator i2, const string& str );    The section is replaced by a copy of the entire string object str.string& replace ( size_t pos1, size_t n1, const string& str, size_t pos2, size_t n2 );    The section is replaced by a copy of a substring of str. The substring is the portion of str that begins at the character position pos2 and takes up to n2 characters (it takes less than n2 if the end of the string is reached before).string& replace ( size_t pos1, size_t n1, const char * s, size_t n2 );string& replace ( iterator i1, iterator i2, const char * s, size_t n2 );    The section is replaced by a copy of the string formed by the first n2 characters in the array of characters pointed by s.string& replace ( size_t pos1, size_t n1, const char * s );string& replace ( iterator i1, iterator i2, const char * s );    The section is replaced by a copy of the string formed by the null-terminated character sequence (C string) pointed by s. The length of this character sequence is determined by the first ocurrence of a null character (as determined by traits.length(s)).string& replace ( size_t pos1, size_t n1, size_t n2, char c );string& replace ( iterator i1, iterator i2, size_t n2, char c );    The section is replaced by a repetition of character c, n2 times.template<class InputIterator> string& replace (iterator i1, iterator i2, InputIterator j1, InputIterator j2);    The section is replaced by the content made up of the characters that go from the element referred by iterator j1 to the element right before the one referred by iterator j2.// replacing in a string#include <iostream>#include <string>using namespace std;int main (){  string base="this is a test string.";  string str2="n example";  string str3="sample phrase";  string str4="useful.";  // function versions used in the same order as described above:  // Using positions:                 0123456789*123456789*12345  string str=base;                // "this is a test string."  str.replace(9,5,str2);          // "this is an example string."  str.replace(19,6,str3,7,6);     // "this is an example phrase."  str.replace(8,10,"just all",6); // "this is just a phrase."  str.replace(8,6,"a short");     // "this is a short phrase."  str.replace(22,1,3,'!');        // "this is a short phrase!!!"  // Using iterators:                      0123456789*123456789*  string::iterator it = str.begin();   //  ^  str.replace(it,str.end()-3,str3);    // "sample phrase!!!"  str.replace(it,it+6,"replace it",7); // "replace phrase!!!"  it+=8;                               //          ^  str.replace(it,it+6,"is cool");      // "replace is cool!!!"  str.replace(it+4,str.end()-4,4,'o'); // "replace is cooool!!!"  it+=3;                               //             ^  str.replace(it,str.end(),str4.begin(),str4.end());                                       // "replace is useful."  cout << str << endl;  return 0;}Output:replace is useful.

Browse random answers:

What are the C-Style Character String?
Exaplain about c_str and example?
Explain about the  string operate data with example ?
Explain about string get_allocator ?
Explain about string copy with example ?
Explain about string find method with example ?
Explain about string rfind method with example ?
Explain about string find_first_of method and with example ?
Explain about find_last_of method with example ?
Explain about find_first_not_of method with example ?
Explain about find_last_not_of method with example ?
Explain about find_last_of method with example ?
Explain about Insert into string method with example ?
Explain about Return length of string method with example ?
Explain about Return maximum size of string method with example ?
Explain about operator+= with example ?
Explain about operator= method with example ?
Explain about  operator[] method with example ?
Explain about push_back method with example ?
Explain about  'rbegin' method with example ?
Explain about rend method with example ?
Explain about replace method with example ?
Explain about reserve method with method ?
Explain about resize method with example ?
Explain about substr method with example ?
Explain about swap method with example ?
Explain about length method with example ?
What is C-string? Explain with an example.?
Problem with C-string  
What are the String Class in C++?
What is "strstream??
Define a class to represent a bank account. In the class, include the following members:Data members: name of the depositor, account number, type of account, and balance amount in the account.Member functions: to assign initial values, to deposit an amount, to withdrawal an amount after checking the balance, and to display the name and balance.Write main() code to test your class?
What is the difference between char a[] = ?string?; and char *p = ?string?;?
How to delete an element in array of string?
 Can you be bale to identify between Straight- through and Cross- over cable wiring? and in what case do you use Straight- through and Cross-over? (Asked by Cisco system people)
In C++ it was easy to manipulate strings. To read character number 6 you just use
How to break the string of characters into words?
How can I make my class to be output into I/O streams?
What’s the difference between a token and a field?
What will be the output of the program ?#include<stdio.h>#include<string.h>int main(){    char str1[20] = "Hello", str2[20] = " World";    printf("%s\n", strcpy(str2, strcat(str1, str2)));    return 0;}
Which of the following function sets first n characters of a string to a given character?
If the two strings are identical, then strcmp() function returns
How will you print \n on the screen?
The library function used to find the last occurrence of a character in a string is
Which of the following function is used to find the first occurrence of a given string in another string?
Which of the following function is more appropriate for reading in a multi-word string?
What will be the output of the program ?#include<stdio.h>#include<string.h>int main(){    char str1[20] = "Hello", str2[20] = " World";    printf("%s\n", strcpy(str2, strcat(str1, str2)));    return 0;}
What follows is C++ code to give you a trimmed copy of a string.
How do I convert a std::string to a number?
How to convertir une string en char* ?
How to  manipuler un tableau de string ?
What are différence entre string::size() et string::capacity() ?
How manipuler un nom de fichier avec string ?
What are différence entre string::find() et string::find_first_of() ?