Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Common String Methods | Text Data Type
C++ Data Types
course content

Зміст курсу

C++ Data Types

C++ Data Types

1. Introduction
2. Numerical Data Types
3. Text Data Type
4. Other Data Types and Concepts

book
Common String Methods

Append

We saw that adding new characters to the end of a string using indexing is not possible. However, there is a convenient .append() method for this purpose.

h

append

copy
1
str.append("added part");

Concatenate

Another way to do it is by using the + operator. It performs concatenation when applied to strings.

cpp

main

copy
12345678
#include <iostream> int main() { std::string str = "Code"; str = str + "finity"; // or str += "finity" std::cout << str << std::endl; }

It also allows adding text to the beginning or to both ends, which .append() is incapable of.

cpp

main

copy
12345678
#include <iostream> int main() { std::string str = "finity"; str = "code" + str + ".com"; std::cout << str << std::endl; }

Insert

You can insert new text into a string at a specific position using the .insert() method.

h

insert

copy
1
str.insert(pos, "text to add");

The position, specified by the pos parameter, determines where the new text will be added.

The new text is inserted before the character currently at the given position. This method is useful for dynamically modifying strings without creating new ones.

Replace

You can also replace a part of a string with a different string. This is achievable using the .replace() method.

h

replace

copy
1
str.replace(start, n, "new text");

Here start means the index of the first element to replace, and n stands for the length of a part to replace.

The following is a gif of how .replace() works.

Erase

You can also remove a portion of a string using the .erase() method. This is useful when you need to delete specific characters or substrings from a string.

h

erase

copy
1
str.erase(start, n);

If the n parameter matches or exceeds the remaining length of the string from start, all characters from start onward will be erased.

The start and n parameters work similarly to those in the .replace() method, providing flexibility to modify the string as needed.

cpp

main

copy
123456789101112131415161718192021
#include <iostream> int main() { std::string str = "finity"; str.append(".com"); // finity to finity.com std::cout << str << std::endl; str = "in" + str; // finity.com to infinity.com std::cout << str << std::endl; str.insert(2, "de"); // infinity.com to indefinity.com std::cout << str << std::endl; str.replace(0, 2, "co"); // indefinity.com to codefinity.com std::cout << str << std::endl; str.erase(10, 4); // codefinity.com to codefinity std::cout << str << std::endl; }

Все було зрозуміло?

Як ми можемо покращити це?

Дякуємо за ваш відгук!

Секція 3. Розділ 4
We're sorry to hear that something went wrong. What happened?
some-alt