In JavaScript, strings are a primitive type. In C++, they're a class that can be constructed with different char
types depending on the kind of string you want.
#include <string>
string foo = "bar";
In order to use strings, you have to import the string
library from std
.
Given that string
is a class, there are a lot of member functions available to be used right out of the box.
In C, you might use a character array to create a string. It's a statically sized array containing each char
you want to represent in your string plus a null character (\0
):
char foo[4] = {'b', 'a', 'r', '\0'};
You can also do this in C++, and it's more performant than using a string class, but using a string class has some advantages like mutability, dynamic memory allocation, and built-in helper functions.