In C++, a string is a sequence of characters. As you know, C++ does not support the built-in string type, and you have previously used null
character-based terminated arrays to store and manipulate strings. These strings are termed C Strings. C++ often becomes inefficient at operating on strings. Programmers can also define their string classes with appropriate member functions for manipulating strings. ANSI standard C++ introduces a new class called string, an improvised version of C strings in several ways. In many cases, the string object may be treated like any other built-in data type. The string is considered another container class for C++.
The C Style String
The C style string belongs to the C language and continues to support C++. Also, strings in C are the one-dimensional array of characters that gets terminated by \0
(null character). Below is an example that shows how strings are declared in C:
char ch[6] = {'H', 'e', 'l', 'l', 'o', '\0'};
In C++, placing the null character at the end of a string constant is not required. The C++ compiler automatically sets the \0
at the end of the string when it initializes the array.
String Class in C++
The string class is vast and includes many constructors, member functions, and operators. Programmers may use the constructors, operators, and member functions to achieve the following:
- Creating string objects.
- Reading string objects from the keyboard.
- Displaying string objects on the screen.
- Finding a substring from a string.
- Modifying string.
- Adding objects of string.
- Comparing strings.
- Accessing characters of a string.
- Obtaining the size or length of a string, etc.
Manipulate Null-terminated Strings
C++ supports a wide range of functions that manipulate null-terminated strings. These are:
- strcpy(str1, str2): Copies string str2 into string str1.
- strcat(str1, str2): Concatenates string str2 onto the end of string str1.
- strlen(str1): Returns the length of string str1.
- strcmp(str1, str2): Returns 0 if str1 and str2 are the same; less than 0 if str1<str2; greater than 0 if str1>str2.
- strchr(str1, ch): Returns a pointer to the first occurrence of character ch in string str1.
- strstr(str1, str2): Returns a pointer to the first occurrence of string str2 in string str1.
Important Functions Supported by String Class
- append(): This function appends a part of a string to another string.
- assign():This function assigns a partial string.
- at(): This function obtains the character stored at a specified location.
- begin(): This function returns a reference to the start of the string.
- capacity(): This function gives the total element that can be stored.
- compare(): This function compares a string against the invoking string.
- empty(): This function returns true if the string is empty.
- end(): This function returns a reference to the end of the string.
- erase(): This function removes characters as specified.
- find(): This function searches for the occurrence of a specified substring.
- length(): It gives the size of a string or the number of elements of a string.
- swap(): This function swaps the given string with the invoking one.
Important Constructors Obtained by String Class
- String(): This constructor is used for creating an empty string.
- String(const char *str): This constructor is used for creating string objects from a null-terminated string.
- String(const string *str): This constructor is used for creating a string object from another string object.
Operators Used for String Objects
Operator | Meaning |
---|---|
=
| Assignment |
+
| Concatenation |
==
| Equality |
!=
| Inequality |
<
| Less than |
<=
| Less than or equal |
>
| Greater than |
>=
| Greater than or equal |
[]
| Subscription |
<<
| Output |
>>
| Input |