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

Contenido del Curso

C++ Data Types

C++ Data Types

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

bookString

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:

cpp

main

copy
1234567
#include <iostream> int main() { char word[10] = {'C', 'o', 'd', 'e', 'f', 'i', 'n', 'i', 't', 'y'}; std::cout << word << std::endl; }

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:

cpp

main

copy
12345678910
#include <iostream> int main() { std::string word = "Codefinity"; std::cout << word << std::endl; word.append(".com"); std::cout << word << std::endl; }

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.

Tarea

  • Create a string variable and store the name 'Alex' in it.
  • Output its value to the console.

Switch to desktopCambia al escritorio para practicar en el mundo realContinúe desde donde se encuentra utilizando una de las siguientes opciones
¿Todo estuvo claro?

¿Cómo podemos mejorarlo?

¡Gracias por tus comentarios!

Sección 3. Capítulo 2
toggle bottom row

bookString

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:

cpp

main

copy
1234567
#include <iostream> int main() { char word[10] = {'C', 'o', 'd', 'e', 'f', 'i', 'n', 'i', 't', 'y'}; std::cout << word << std::endl; }

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:

cpp

main

copy
12345678910
#include <iostream> int main() { std::string word = "Codefinity"; std::cout << word << std::endl; word.append(".com"); std::cout << word << std::endl; }

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.

Tarea

  • Create a string variable and store the name 'Alex' in it.
  • Output its value to the console.

Switch to desktopCambia al escritorio para practicar en el mundo realContinúe desde donde se encuentra utilizando una de las siguientes opciones
¿Todo estuvo claro?

¿Cómo podemos mejorarlo?

¡Gracias por tus comentarios!

Sección 3. Capítulo 2
toggle bottom row

bookString

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:

cpp

main

copy
1234567
#include <iostream> int main() { char word[10] = {'C', 'o', 'd', 'e', 'f', 'i', 'n', 'i', 't', 'y'}; std::cout << word << std::endl; }

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:

cpp

main

copy
12345678910
#include <iostream> int main() { std::string word = "Codefinity"; std::cout << word << std::endl; word.append(".com"); std::cout << word << std::endl; }

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.

Tarea

  • Create a string variable and store the name 'Alex' in it.
  • Output its value to the console.

Switch to desktopCambia al escritorio para practicar en el mundo realContinúe desde donde se encuentra utilizando una de las siguientes opciones
¿Todo estuvo claro?

¿Cómo podemos mejorarlo?

¡Gracias por tus comentarios!

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:

cpp

main

copy
1234567
#include <iostream> int main() { char word[10] = {'C', 'o', 'd', 'e', 'f', 'i', 'n', 'i', 't', 'y'}; std::cout << word << std::endl; }

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:

cpp

main

copy
12345678910
#include <iostream> int main() { std::string word = "Codefinity"; std::cout << word << std::endl; word.append(".com"); std::cout << word << std::endl; }

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.

Tarea

  • Create a string variable and store the name 'Alex' in it.
  • Output its value to the console.

Switch to desktopCambia al escritorio para practicar en el mundo realContinúe desde donde se encuentra utilizando una de las siguientes opciones
Sección 3. Capítulo 2
Switch to desktopCambia al escritorio para practicar en el mundo realContinúe desde donde se encuentra utilizando una de las siguientes opciones
some-alt