How can I use replace() method of std::string in a C++ app? How can I replace a string into another string? Can we replace a char array into a string? How can I replace a number of chars into a string? Is it possible to replace a string with a substring of another string? Here are the answers with C++ examples.
Table of Contents
First, let’s recap on how strings work in a C++ app
In addition to int, float, double there is another data type, string that we use for alphanumeric variables. In C++ there are several typedefs for common character types. String types are defined in the header <string>.
strings are the string class for byte characters represented with string. Usually, alphanumeric characters are stored and displayed in string forms. In other words, string stores the alphanumeric text with 1-byte chars, called ASCII chars. Strings are the instantiation of the basic_string class template that uses char as the character type. Simply we can define a string as below,
1 2 3 |
std::string str = "This is a String"; |
string has methods to assign, copy, align, replace or to operate with other strings. These methods can be used in all string methods with their appropriate syntax. We can replace a string into another string in different methods. Let’s see some of most used methods.
Replacing a String into another String with the replace() Method
The replace() method of std::string, replaces the part of the string indicated by either start and end index positions with a new string. That means the string between start and end index positions will be replaced with a new string. We can use replace() method with the range parameter, just give the start position index and replacement count to define range (start_position, replace_count, where to replace in range ) and the string to be replaced (replace_str). Here is syntax of a replace() method,
1 2 3 |
basic_string& replace( size_type start_position, size_type replace_count, const basic_string& replace_str ); // Until C++20 |
Here is a example how we can use replace() method with in this given syntax, see example below,
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
#include <iostream> #include <string> int main() { std::string s1, s2; s1 = "first nice string"; s2 = "and the second"; s1.replace( 6, 4, s2 ); // Replaces s2 to the from the position 6 to 6+4 of the s1 std::cout << s1 << std::endl; getchar(); return 0; } |
and the output will be,
1 2 3 |
first and the second string |
Getting your C++ app to replace a Char Array into a String with the replace() Method
replace() method of std::string, replaces the part of the string indicated by either start and end index positions with a new string. That means the string between start and end index positions will be replaced with a new string. We can use replace() method with the range parameter, just give the start position index and replacement count to define range (start_position, replace_count, where to replace in range ) and the string to be replaced (replace_str). Here is syntax of a replace() method,
1 2 3 |
basic_string& replace( size_type start_position, size_type replace_count, char* replace_str ); // Until C++20 |
Here is a example how we can use replace() method with in this given syntax, see example below,
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
#include <iostream> #include <string> int main() { std::string s1 = "first nice string"; char s[] = "and the second"; s1.replace( 6, 4, s ); // Replaces s2 to the from the position 6 to 6+4 of the s1 std::cout << s1 << std::endl; string_replace2(); getchar(); return 0; } |
and the output will be,
1 2 3 |
first and the second string |
Replacing a given quantity of Chars into a String with the replace() Method
replace() method of std::string, can be used to replace chars in given numbers. To do this we should use parameters to indicate by either start and end index positions (start_position, end_position, where to replace) and the char count (char_count) and the char in ‘ ‘ (replace_char). Here is syntax of a replace() method,
1 2 3 |
basic_string& replace( size_type start_position, size_type replace_count, size_type char_count, CharT replace_char ); // Until C++20 |
Here is a example how we can use replace() method with in this given syntax, see example below,
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
#include <iostream> #include <string> int main() { std::string s1 = "first nice string"; char s[] = "and the second"; s1.replace( 6, 4, s ); // Replaces s2 to the from the position 6 to 6+4 of the s1 std::cout << s1 << std::endl; string_replace2(); getchar(); return 0; } |
and the output will be,
1 2 3 |
first ---------- string |
Replacing a Substring in a C++ app into another String with the replace() Method
replace() method of std::string, can be used to replace a substring into another string. To do this we should use parameters to indicate by either start and end index positions (start_position, end_position, where to replace) and the char count (char_count) and the sub string. Here is syntax of a replace() method,
Here is a example how we can use replace() method with in this given syntax, see example below,
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
#include <iostream> #include <string> int main() { std::string s1, s2; s1 = "first nice string"; s2 = "and the second"; s1.replace( 6, 4, s2.substr(4,3) ); // Replaces s2 starts from position 4 to 4+3 to the position 6 of the s1 in range 4 std::cout << s1 << std::endl; getchar(); return 0; } |
and the output will be,
1 2 3 |
first the string |
Note that, this replace method can be used with std::wstring too.
Design. Code. Compile. Deploy.
Start Free Trial
Free C++Builder Community Edition