C++ - String Functions


Advertisements

Previous Page Next Page

C++ Strings

Strings are one of the most common data types in C++. It is used for storing text. It can be created by enclosing characters in double quotation marks. It can be assigned to a variable using = sign.

string MyString = "Hello World!";

Access character of a String

A character (also called element) of a string can be accessed with it's index number. In C++, index number starts with 0 in forward direction. The below figure and example describe the indexing concept of a string.

C++ - String Functions
FunctionsDescription
length() Returns the length of the string.
size() Returns the length of the string in terms of bytes.
capacity() Returns size of allocated space to the string.
max_size() Returns the maximum length of the string.
back() Returns the reference of last character of the string.
begin() Returns the reference of first character of the string.
clear() Clears all elements of the string.
empty() Returns the length of the string in terms of bytes.
pop_back() Removes the last character of the string.
push_back() Add a new character at the end of the string.
compare() Compares two string objects.

Previous Page Next Page
Advertisements