How can I use replace() method of std::wstring in my C++ app? How can I replace a wstring into another wstring? Can we replace char array to a wstring? How can I replace number of chars to a wstring? Is it possible to replace a wide string with a substring of another wide string? Here are the answers with C++ examples.
Generally, as an introduction to C++, in addition to int, float, double, string there is another data type wstring in modern C++ that we can use for alphanumeric variables. In Modern C++, there are several typedefs for common character types are provided: Wide string types are defined in header <string>.
wstrings (Wide Strings) are the string class for characters that has 2 bytes that represented with wstring. In Modern C++, alphanumeric characters are stored in wide strings because of their supports to characters of world languages and displayed in wstring forms. In another terms wstring stores for the alphanumeric text with 2 byte chars, called wchar_t or WChar. Wide strings are the instantiation of the basic_string class template that uses wchar_t as the character type. Simply we can define a wstring as below,
1 2 3 |
std::wstring str = L"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.
Table of Contents
You can also use the replace method of std::string in your C++ app
We’ve written about the corresponding replace method for std::string here:
How to replace a Wide String in your C++ app into another Wide String with the replace() Method
replace() method of std::wstring, replaces the part of the string indicated by either start and end index positions with a new wide string. That means the string between start and end index positions will be replaced with a new wide 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 the syntax of a replace() method,
Syntax:
1 2 3 4 |
basic_string& replace (size_type position, size_type len, const basic_string& str); basic_string& replace (const_iterator iter1, const_iterator iter2, const basic_string& str); |
This example below replace L”amazing” to wstr1 after “This is “, overwrites into “C++” data.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
#include <iostream> #include <string> int main() { std::wstring wstr = L"This is C++"; wstr.replace(8, 8, L"amazing"); std::wcout << wstr << '\n'; getchar(); return 0; } |
Output will be as below,
1 2 3 |
This is amazing |
Replacing a Char array into a Wide string with the replace() method
replace() method of std::wstring, replaces the part of the wstring indicated by either start and end index positions with a new wide string. That means the wide string between start and end index positions will be replaced with a new wide 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 wide string to be replaced (replace_str). Here is the 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::wstring s1 = L"the first nice string"; char s[] = "and the second"; std::wstring ws( &s[0], &s[strlen(s)]); s1.replace( 10, 4, ws ); // Replaces ws to the from the position 10 to 10+4 of the s1 std::wcout << s1 << std::endl; getchar(); return 0; } |
and the output will be,
1 2 3 |
the first and the second string |
Replacing a given number of Chars into another Wide string with the replace() Method
You can get your C++ app to manipulate strings with the replace() method of std::wstring. It 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 the 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 |
#include <iostream> #include <string> int main() { std::wstring s1 = L" the first nice string"; s1.replace( 10, 4, 8, '-' ); // Replaces 8 '-' chars to the position 10 - 10+4 of the s1 std::wcout << s1 << std::endl; getchar(); return 0; } |
and the output will be,
1 2 3 |
the first -------- string |
How to place a substring into another Wide string with the replace() method
replace() method of std::wstring, can be used to replace a substring into another wide 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 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::wstring s1, s2; s1 = L"the first nice string"; s2 = L"and the second"; s1.replace( 10, 4, s2.substr(4,3) ); // Replaces s2 to the position 10 of the s1 std::wcout << s1 << std::endl; getchar(); return 0; } |
and the output will be,
1 2 3 |
the first the string |
Note that, this replace method can be used with std::string too.
Design. Code. Compile. Deploy.
Start Free Trial
Free C++Builder Community Edition