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

How To Use Methods To Copy One String to Another String

How can we copy one string to another string? Here are the answers with C++ examples.

Generally, as an introduction to C++, 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 are provided: String types are defined in header <string>.

strings are the string class for byte characters represented with string and alphanumeric characters are stored and displayed in string forms. In another terms string stores for 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. Here are the string types defined in std namespace with their char type and C++ standard.

How to copy a string to another string in C++ with = operator

We can use ‘=’ operator to copy strings as given example below,

How to use the C++ assign() method to copy strings

We can copy string to another string with assign() method as below,

How to copy a C++ String to a Char array

We can copy a string to char array in given number of chars

Note that we can also use these methods to copy wstring in C++.

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++11C++14C++17C++20Learn C++

What Is The Priority Queue (std::priority_queue) In Modern C++?

C++C++11C++14C++17C++20

What Is The Stack (std::stack) In Modern C++?

C++C++11C++14C++17C++20Learn C++

What Is The Queue (std::queue) In Modern C++?

C++C++11C++14C++17Learn C++SyntaxTemplates

What Are The Logical Operation Metafunctions In Modern C++?