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

How To Use Basic Methods Of Strings In C++?

How To Use Basic Methods Of Strings In C++

In C++, one of the most used variable types are text strings, also known as alfa-numeric variables, and they are really important when storing and retrieving valuable data. It is important to store your data safely in its language and localization. In modern C++, strings are std::basic_string types such as std::string, and std::wstring. In this post, we explain some methods of std::string in C++, we will explain copy, assign, append, insert, and replace methods of std::string that you can use in string operations.

What is a basic string (std::basic_string) in C++?

The basic_string (std::basic_string and std::pmr::basic_string) is a class template that stores and manipulates sequences of alpha numeric string objects (char,w_char,…). For example, str::string and std::wstring are the data types defined by the std::basic_string<char>. In other words, basic_string is used to define different data_types which means a basic_string is not a string only, it is a namespace for a general string format. A basic string can be used to define string, wstring, u8string, u16string and u32string data types. Here are all you need to know about std::basic_string,

What is a string (std::string) in C++?

In C++, 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. Now, let’s see them.

How can we copy a string to another string in C++?

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

here are more examples,

How can we assign a string to a string in C++? (Copying a string with the assign method)

The assign() method of a string, is a method of basic_string ( here it is string) assigns a new string to the current string, it is replacing its contents to contents of a new string. Here is the syntax,

See example below,

or we can copy a string to another one by using assign() method,

Output will be as below,

How can we use the append method in strings in C++? (Appending a string to a string)

We can use append() method to append text with “…” or to append another string as given syntax below.

Syntax:

This example below appends ” with appended text” to wstr1 and then appends wstr2,

Output will be as below,

Here are more examples about to append one string to another.

How can we use the insert method with strings in C++? (Inserting a string into a string)

We can use insert() method to append text with “…” or to append another string as given syntax below.

Syntax:

This example below inserts ” amazing” to str, and then print outs this string,

Output will be as below,

Here are more examples,

How can we use the replace method with strings in C++? (Replacing a string with a string)

We can use replace() method to replace a text with “…” or to replace another string as given syntax below.

Syntax:

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

Output will be as below,

Here are more examples,

As in given these string methods above, these methods can be used on wstrings methods in the same syntax. These examples can run on C++ Builder, Visual C++, Dev-C++, GNU C++ and other C++ compilers that supports CLANG standards. Don’t forget to add L”” literal when using wide strings in operations.

There are more string methods and properties in modern c++. Here are all posts about strings: https://learncplusplus.org/?s=string

How To Use Basic Methods Of Strings In C++ C++ logo

C++ Builder is the easiest and fastest C and C++ compiler and IDE for building simple or professional applications on the Windows operating system. It is also easy for beginners to learn with its wide range of samples, tutorials, help files, and LSP support for code. RAD Studio’s C++ Builder version comes with the award-winning VCL framework for high-performance native Windows apps and the powerful FireMonkey (FMX) framework for UIs.

There is a free C++ Builder Community Edition for students, beginners, and startups; it can be downloaded from here. For professional developers, there are Professional, Architect, or Enterprise versions of C++ Builder and there is a trial version you can download from here.

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.

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++20Introduction to C++Learn C++Syntax

How To Use Basic Methods Of Wide Strings In C++?

C++C++17C++20C++23Learn C++Syntax

How To Use std::make_from_tuple in C++ 17

C++C++17C++20Learn C++

How To Use std::apply With Tuple In C++ 17 And Beyond?

C++C++17C++20Learn C++NumericsSyntax

How To Compute The Greatest Common Divisor And Least Common Multiple in C++?