Contenido del Curso
C++ Data Types
2. Numerical Data Types
4. Other Data Types and Concepts
C++ Data Types
Working With String
Append
As you saw in the previous chapter, we cannot add new characters to the end of a string using indexing. But C++ has a neat .append()
method for that. Here is the syntax:
Concatenate
Another way to do it is by using the +
operator.
It performs concatenation when applied to strings. Here is an example:
main.cpp
It also allows adding text to the beginning or to both ends, which .append()
is incapable of.
main.cpp
Insert
You can also insert new text to a string at a desired position.
Here is the syntax:
Where pos
is an index, before which, new text is inserted.
Here is a gif of how insertion works:
Replace
You can also replace a part of a string with a different string.
This is achievable using the .replace()
method. Here is the syntax:
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 erase part of a string. Here is the syntax:
start
and n
parameters have the same meaning as in the .replace()
method.
In this chapter, you learned
Method | Description |
---|---|
.append(str2) |
Adds new characters to the end of a string |
+ operator |
Concatenates strings |
insert(pos, str2) |
Inserts new characters at a given position |
replace(start, n, str2) |
Replaces part of a string with new characters |
erase(start, n) |
Erases part of a string |
Here is an example of using all of the methods.
main.cpp
¿Todo estuvo claro?
Contenido del Curso
C++ Data Types
2. Numerical Data Types
4. Other Data Types and Concepts
C++ Data Types
Working With String
Append
As you saw in the previous chapter, we cannot add new characters to the end of a string using indexing. But C++ has a neat .append()
method for that. Here is the syntax:
Concatenate
Another way to do it is by using the +
operator.
It performs concatenation when applied to strings. Here is an example:
main.cpp
It also allows adding text to the beginning or to both ends, which .append()
is incapable of.
main.cpp
Insert
You can also insert new text to a string at a desired position.
Here is the syntax:
Where pos
is an index, before which, new text is inserted.
Here is a gif of how insertion works:
Replace
You can also replace a part of a string with a different string.
This is achievable using the .replace()
method. Here is the syntax:
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 erase part of a string. Here is the syntax:
start
and n
parameters have the same meaning as in the .replace()
method.
In this chapter, you learned
Method | Description |
---|---|
.append(str2) |
Adds new characters to the end of a string |
+ operator |
Concatenates strings |
insert(pos, str2) |
Inserts new characters at a given position |
replace(start, n, str2) |
Replaces part of a string with new characters |
erase(start, n) |
Erases part of a string |
Here is an example of using all of the methods.
main.cpp
¿Todo estuvo claro?