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

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


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.

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,

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,

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

and the output will be,

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,

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 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,

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 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,

and the output will be,

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

close

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

33+ years of coding with more than 30+ programming languages, mostly C++ on Windows, Android, Mac-OS, iOS, Linux and some other operating systems. Dr. Yilmaz Yoru was born in 1974, Eskisehir-Turkey. He graduated from the department of Mechanical Engineering of Eskisehir Osmangazi University in 1997. One year later he started to work in the same university as an assistant. He received his MSc and PhD degrees from the same department of the same university. He has married and he is a father of a son. Some of his interests are Programming, Thermodynamics, Fluid Mechanics and Artificial Intelligence. He also likes the graphical 2D & 3D design and high-end innovations.
Related posts
C++C++11C++14C++17Introduction to C++Learn C++

How To Use WString find_first_of() In A Modern C++ App

C++Learn C++

How To Read An XML File In A C Program Or C++ App

C++Learn C++

Solve A 3 Diagonal Matrix Using Successive Over Relaxation

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

How To Make Use Of Wide String Properties In C++ Software