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

Course Content

C++ Data Types

C++ Data Types

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

bookString

Instead of working with individual characters, we usually work with complete words, sentences, or texts sequences of characters. One way to represent these sequences is with a char array, like this:

cpp

main

copy
12345678910
#include <iostream> int main() { // The '\0' is a null terminator, used to indicate the end of the string // It ensures that the array of characters is treated as a proper string char word[6] = { 'H', 'e', 'l', 'l', 'o', '\0' }; std::cout << word << std::endl; }

As you can see, initializing a character array can be tricky. Moreover, modifying the text, such as adding more characters, requires redefining the array with additional memory.

Thankfully, you can use the string class, which simplifies this process significantly.

h

string

copy
1
std::string text = "Any text in double quotes";

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
123456789101112
#include <iostream> int main() { // Declare and initialize a string std::string word = "Codefinity"; std::cout << word << std::endl; // Use the append() method to add ".com" to the string. 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.

Task
test

Swipe to show code editor

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

Switch to desktopSwitch to desktop for real-world practiceContinue from where you are using one of the options below
Everything was clear?

How can we improve it?

Thanks for your feedback!

Section 3. Chapter 2
toggle bottom row

bookString

Instead of working with individual characters, we usually work with complete words, sentences, or texts sequences of characters. One way to represent these sequences is with a char array, like this:

cpp

main

copy
12345678910
#include <iostream> int main() { // The '\0' is a null terminator, used to indicate the end of the string // It ensures that the array of characters is treated as a proper string char word[6] = { 'H', 'e', 'l', 'l', 'o', '\0' }; std::cout << word << std::endl; }

As you can see, initializing a character array can be tricky. Moreover, modifying the text, such as adding more characters, requires redefining the array with additional memory.

Thankfully, you can use the string class, which simplifies this process significantly.

h

string

copy
1
std::string text = "Any text in double quotes";

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
123456789101112
#include <iostream> int main() { // Declare and initialize a string std::string word = "Codefinity"; std::cout << word << std::endl; // Use the append() method to add ".com" to the string. 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.

Task
test

Swipe to show code editor

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

Switch to desktopSwitch to desktop for real-world practiceContinue from where you are using one of the options below
Everything was clear?

How can we improve it?

Thanks for your feedback!

Section 3. Chapter 2
toggle bottom row

bookString

Instead of working with individual characters, we usually work with complete words, sentences, or texts sequences of characters. One way to represent these sequences is with a char array, like this:

cpp

main

copy
12345678910
#include <iostream> int main() { // The '\0' is a null terminator, used to indicate the end of the string // It ensures that the array of characters is treated as a proper string char word[6] = { 'H', 'e', 'l', 'l', 'o', '\0' }; std::cout << word << std::endl; }

As you can see, initializing a character array can be tricky. Moreover, modifying the text, such as adding more characters, requires redefining the array with additional memory.

Thankfully, you can use the string class, which simplifies this process significantly.

h

string

copy
1
std::string text = "Any text in double quotes";

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
123456789101112
#include <iostream> int main() { // Declare and initialize a string std::string word = "Codefinity"; std::cout << word << std::endl; // Use the append() method to add ".com" to the string. 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.

Task
test

Swipe to show code editor

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

Switch to desktopSwitch to desktop for real-world practiceContinue from where you are using one of the options below
Everything was clear?

How can we improve it?

Thanks for your feedback!

Instead of working with individual characters, we usually work with complete words, sentences, or texts sequences of characters. One way to represent these sequences is with a char array, like this:

cpp

main

copy
12345678910
#include <iostream> int main() { // The '\0' is a null terminator, used to indicate the end of the string // It ensures that the array of characters is treated as a proper string char word[6] = { 'H', 'e', 'l', 'l', 'o', '\0' }; std::cout << word << std::endl; }

As you can see, initializing a character array can be tricky. Moreover, modifying the text, such as adding more characters, requires redefining the array with additional memory.

Thankfully, you can use the string class, which simplifies this process significantly.

h

string

copy
1
std::string text = "Any text in double quotes";

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
123456789101112
#include <iostream> int main() { // Declare and initialize a string std::string word = "Codefinity"; std::cout << word << std::endl; // Use the append() method to add ".com" to the string. 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.

Task
test

Swipe to show code editor

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

Switch to desktopSwitch to desktop for real-world practiceContinue from where you are using one of the options below
Section 3. Chapter 2
Switch to desktopSwitch to desktop for real-world practiceContinue from where you are using one of the options below
We're sorry to hear that something went wrong. What happened?
some-alt