News & UpdatesProgrammingWeb programming StoreMy Projects

C++ Tutorial – 09 – String

The string class in C++ is used to store string values. Before a string can be declared the string header must first be included. The standard namespace can also be included since the string class is part of that namespace.

#include <string>
using namespace std;

Strings can then be declared as any other data type. To assign a string value to a string variable, delimit the literals by double quotes and assign them to the variable. The initial value can also be assigned through constructor initialization at the same time as the string is declared.

string h = "Hello";
string w (" World");

String combining

The plus sign is used to combine two strings, known as the concatenation operator (+) in this context. It has an accompanying assignment operator (+=) to append a string.

string a = h + w; // Hello World
h += w; // Hello World

The concatenation operator will work as long as one of the strings it operates on is a C++ string.

string b = "Hello" + w; // ok

It is not able to concatenate two C strings or two string literals. To do this, one of the values has to be explicitly cast to a string.

char *c = "World"; // C-style string
b = (string)c + c; // ok
b = "Hello" + (string)" World"; // ok

String literals are implicitly combined and must not have a plus sign between them.

b = "Hel" "lo"; // ok

Escape characters

A string literal can be extended to more than one line by putting a backslash sign (\) at the end of each line.

string s = "Hello \

To add a new line to the string itself, the escape character “\n” is used.

s = "Hello \n World";

This backslash notation is used to write special characters, such as tab or form feed characters.

\nnewline\fform feed
\thorizontal tab\aalert sound
\vvertical tab\’single quote
\bbackspace\”double quote
\rcarriage return\\backslash
\0null character  

Additionally, anyone of the 128 ASCII characters can be expressed by writing a backslash followed by the ASCII code for that character, represented as either an octal or hexadecimal number.

"\0177"   // octal character (0-0177)
"\0x7F" // hexadecimal character (0-0x7F)

String compare

The way to compare two strings is simply by using the equal to operator (==). This will not compare the memory addresses of the strings, as is the case of C strings.

string s = "Hello";
bool b = (s == "Hello"); // true

String functions

The string class has a lot of functions. Among the most useful ones are the length and size functions, which both return the number of characters in the string. Their return type is size_t, which is an unsigned data type used to hold the size of an object. This is simply an alias for one of the built-in data types, but which one it is defined as varies between compilers. The alias is defined in the crtdefs.h standard library file, which is included through iostream.

size_t i = s.length(); // 5, length of string
i = s.size(); // 5, same as length()

Another useful function is substr (substring), which requires two arguments. The second argument is the number of characters to return starting from the position specified in the first argument.

s.substr(0,2); // "He"

A single character can also be extracted or changed by using the array notation.

char c = s[0]; // 'H'
Recommended additional reading:
Sams - Teach Yourself C++ in One Hour a Day