C++C++11C++14C++17Introduction to C++Learn C++

How To Replace A Wide String Into Another In A C++ App

How To Replace A Wide String Into Another In A C++ App

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,

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.

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:

This example below replace L”amazing” to wstr1 after “This is “, overwrites into “C++” data.

Output will be as below,

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,

Here is a example how we can use replace() method with in this given syntax, see example below,

and the output will be,

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,

Here is a example how we can use replace() method with in this given syntax, see example below,

and the output will be,

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,

and the output will be,

Note that, this replace method can be used with std::string too.

Oh hi there 👋
It’s nice to meet you.

Sign up to receive awesome C++ content in your inbox, every day.

We don’t spam! Read our privacy policy for more info.


Reduce development time and get to market faster with RAD Studio, Delphi, or C++Builder.
Design. Code. Compile. Deploy.
Start Free Trial

Free C++Builder Community Edition

About author

Dr. Yilmaz Yoru has 35+ years of coding with more than 30+ programming languages, mostly C++ on Windows, Android, Mac-OS, iOS, Linux, and some other operating systems. He graduated and received his MSc and PhD degrees from the Department of Mechanical Engineering of Eskisehir Osmangazi University. He is the founder and CEO of ESENJA LLC Company. His interests are Programming, Thermodynamics, Fluid Mechanics, Artificial Intelligence, 2D & 3D Designs, and high-end innovations.
Related posts
C++C++17Language FeatureLearn C++

How To Use Skia Shader SkSL Shading Language Code in C++ Builder?

Artificial Intelligence TechC++C++17C++20Learn C++

How To Create Simple Generative AI Code In C++

C++C++17Language FeatureLearn C++

How To Use The SVG Image Format With Skia In C++ Builder

C++C++17Language FeatureLearn C++

How To Use Skia Images in C++ Builder?