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.
Table of Contents
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,
1 2 3 |
std::string str = "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. 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,
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
#include <iostream> #include <string> int main() { std::string s1,s2; s1 = "first string"; s2 = "second string"; s1 = s2; // copying s2 to s1 std::cout << s1 << '\n'; std::cout << s2 << '\n'; getchar(); return 0; } |
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,
1 2 3 |
basic_string& assign (const basic_string& str); |
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::string str = "This is my string"; str.assign( "New assigned string"); std::cout << str << "'\n"; getchar(); return 0; } |
or we can copy a string to another one by using assign() method,
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
#include <iostream> #include <string> int main() { std::string str1 = "This is my string"; std::string str2 = "New assigned string"; str1.assign(str2); // Copy str2 to str1 std::cout << str1 << "'\n"; getchar(); return 0; } |
Output will be as below,
1 2 3 |
New assigned string |
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:
1 2 3 |
basic_string& append (const basic_string& str); |
This example below appends ” with appended text” to wstr1 and then appends wstr2,
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::string str1 = "This is my string"; std::string str2 = " and another string"; str1.append(" with appended text"); str1.append(wstr2); std::cout << "String1 = '" << str1 << "'\n"; getchar(); return 0; } |
Output will be as below,
1 2 3 |
New String = 'This is my string with appended text and another string' |
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:
1 2 3 |
basic_string& insert (size_type pos, const basic_string& str); |
This example below inserts ” amazing” to str, and then print outs this string,
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
#include <iostream> #include <string> int main() { std::string str = "This is C++"; str.insert(7, " amazing"); std::cout << str << '\n'; getchar(); return 0; } |
Output will be as below,
1 2 3 |
This is amazing C++ |
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:
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 “amazing” to str1 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::string str = "This is C++"; str.replace(8, 8, "amazing"); std::cout << str << '\n'; getchar(); return 0; } |
Output will be as below,
1 2 3 |
This is amazing |
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
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.
Design. Code. Compile. Deploy.
Start Free Trial
Free C++Builder Community Edition