Alterando uma String
Anexar
Como você viu no capítulo anterior, não podemos adicionar novos caracteres ao final de uma string usando indexação.
Mas o C++ possui um prático método .append()
para isso. Aqui está a sintaxe:
Concatenar
Outra forma de fazer isso é usando o operador +
.
Ele realiza a concatenação quando aplicado a strings. Aqui está um exemplo:
main.cpp
Ele também permite adicionar texto ao início ou a ambas as extremidades, o que .append()
é incapaz de fazer.
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.
Aqui está um exemplo de como usar todos os métodos.
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 |
Aqui está um exemplo de como usar todos os métodos.
main.cpp
Tudo estava claro?
Conteúdo do Curso
C++ Data Types
1. Tipos de Dados Numéricos
2. Numerical Data Types
4. Outros Tipos de Dados e Conversão de Tipos
C++ Data Types
Alterando uma String
Anexar
Como você viu no capítulo anterior, não podemos adicionar novos caracteres ao final de uma string usando indexação.
Mas o C++ possui um prático método .append()
para isso. Aqui está a sintaxe:
Concatenar
Outra forma de fazer isso é usando o operador +
.
Ele realiza a concatenação quando aplicado a strings. Aqui está um exemplo:
main.cpp
Ele também permite adicionar texto ao início ou a ambas as extremidades, o que .append()
é incapaz de fazer.
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.
Aqui está um exemplo de como usar todos os métodos.
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 |
Aqui está um exemplo de como usar todos os métodos.
main.cpp
Tudo estava claro?