Conteúdo do Curso
C++ Data Types
2. Numerical Data Types
4. Other Data Types and Concepts
C++ Data Types
String
Typically, rather than dealing with individual characters, we want to work with complete words, sentences, and texts, which are composed of a sequence of characters.
One way to do that is to use an array of char
's like this:
main.cpp
You can see that initialization is quite tricky. Additionally, to add something to this text, you will need to redefine the array with more allocated memory. Fortunately, in C++, there is a string
class that makes this process much easier.
So you can assign to a string
any text within double quotes " "
. Also, adding more text to a string
is as easy as using the .append()
method. Here is an example:
main.cpp
Besides .append()
, there are many other methods of a string
to allow you efficiently operate with text data. Here is the table with some. They will be discussed in more detail in later chapters.
Method | Description |
---|---|
.append(str2) |
Adds new characters at the end of a string |
.insert(pos, str2) |
Adds new characters at the desired position |
.replace(start, n, str2) |
Replaces part of a string with new characters |
.erase(start, n) |
Erases part of a string |
.at(pos) |
Returns the character at desired position |
.swap(str2) |
Swaps two strings |
.find(str2) |
Finds the first occurrence of a substring |
.rfind(str2) |
Finds the last occurrence of a substring |
.length() |
Returns the length of a string |
.capacity() |
Returns the size of allocated storage |
.reserve(n) |
Allocates memory to hold n characters |
.shrink_to_fit() |
Shrinks the capacity to a string's length |
Tarefa
- Create a string variable and store the name 'Alex' in it.
- Output its value to the console.
Tudo estava claro?
Conteúdo do Curso
C++ Data Types
2. Numerical Data Types
4. Other Data Types and Concepts
C++ Data Types
String
Typically, rather than dealing with individual characters, we want to work with complete words, sentences, and texts, which are composed of a sequence of characters.
One way to do that is to use an array of char
's like this:
main.cpp
You can see that initialization is quite tricky. Additionally, to add something to this text, you will need to redefine the array with more allocated memory. Fortunately, in C++, there is a string
class that makes this process much easier.
So you can assign to a string
any text within double quotes " "
. Also, adding more text to a string
is as easy as using the .append()
method. Here is an example:
main.cpp
Besides .append()
, there are many other methods of a string
to allow you efficiently operate with text data. Here is the table with some. They will be discussed in more detail in later chapters.
Method | Description |
---|---|
.append(str2) |
Adds new characters at the end of a string |
.insert(pos, str2) |
Adds new characters at the desired position |
.replace(start, n, str2) |
Replaces part of a string with new characters |
.erase(start, n) |
Erases part of a string |
.at(pos) |
Returns the character at desired position |
.swap(str2) |
Swaps two strings |
.find(str2) |
Finds the first occurrence of a substring |
.rfind(str2) |
Finds the last occurrence of a substring |
.length() |
Returns the length of a string |
.capacity() |
Returns the size of allocated storage |
.reserve(n) |
Allocates memory to hold n characters |
.shrink_to_fit() |
Shrinks the capacity to a string's length |
Tarefa
- Create a string variable and store the name 'Alex' in it.
- Output its value to the console.
Tudo estava claro?