C++ Strings, String Functions and String Operators
The C++ String Class
A class is special data type with built-in functions. The
ifstream
and ofstream
data types you learned earlier are
examples of classes. They have built-in information and functions to
help you manage files.
C++ has a
string
class with functions and helpful
information that help you manage text data. When you declare a variable
from a class, that variable can also be called an object. Functions
that are built into a class can be called from declared objects using a
special dot notation. You have already seen examples of this with
the .open()
, .get()
, and .close()
functions. C++ classes can also be
enhanced with special operators. In this section we will introduce some
string class functions and operators.
Depending on the IDE you are using, you may need to add #include <string.h> to the top of your program, or strings will not work.
Declaring a String
Strings are declared like any other variable. A string may be declared with or without an initial value. If you do not provide an initial value, the the result is an empty string (zero length, no characters).
Here are some ways to declare and initialise C++ strings:
//declare an empty string called str1 string str1; //When you declare a string variable object, //you may initialize it with a character string literal: string str2 = "Hello there"; // Initialize form 1: Declare and assign string str3 ("Goodbye"); // Initialize form 1: Declare and construct //A string variable/object may also be initialized with //a string expression: string str4 = str2; string str5 = str2 + str3; //A string variable/object may also be initialized with //a sub string of another string object: string str6 = "ABCDEFGHIJKL"; // Initialize str7 as "CDEFG" // Starts at character 2 ('C') // with a length of 5 // (or the rest of the string, if shorter) string str7 (str6,2,5);
The .length()
and .size()
Functions
Both of these functions return the number of characters in the string.
They return a special type: size_type
.
It is an unsigned integer. We use the qualified name string::size_type
because
the definition of
size_type
is otherwise hidden inside the definition of the string type.
string str8 = "Hello"; string::size_type len; //Store returned values in a variable, then use them: len = str8.length(); cout << len << endl; // prints 5 len = str8.size(); cout << len << endl; // also prints 5 // OR just use them directly: cout << str8.length() << endl;
The .find()
and .substr()
Functions
The .find()
function searches the string it
is called on to find the first occurance of a particular substring.
- If the substring is found,
.find()
returns the position of the first character.
If not,.find()
returns the special valuestring::npos
.
- The first argument is the search substring.
It is a string or string literal.
- The second argument is where to start searching.
It is an integer greater than or equal to 0.
If you leave it out,.find()
starts at 0.
For example:
string str16 = "abcdefghi"; string str17 = "def"; // Search from the beginning of str16 string::size_type pos = str16.find(str17); cout << pos << endl; // prints 3 // Search from the beginning of str16 pos = str16.find(str17,0); cout << pos << endl; // prints 3 // Search from the fifth position of str16 pos = str16.find(str17,5); cout << pos << endl; // prints a REALLY BIG number!! pos = str16.find ("AB"); if (pos == string::npos) { cout << "Not found." << endl; return 1; }
The .substr()
function cuts a substring out of a string.
- The
.substr()
function always returns a substring of the string it is called on - the first parameter is the start position of the substring
- the other parameter is is the length of the substring.
If you leave it out,.substr()
returns everything from the start positon to the end of the string.
For example:
string str18 = "abcdefghi"; string str19 = str18.substr(5,2); cout << str19 << endl; // prints "fg" string str20 = str18.substr(5); cout << str20 << endl; // prints "fghi"
String Operators
A number of C++ operators also work with strings.
- The
=
(assignment) operator may be used in several ways://Assigning one string's value to another string string string_one = "Hello"; string string_two; string_two = string_one;
- The
+
(plus) operator concatenates:// two strings string str1 = "Hello "; string str2 = "there"; string str3 = str1 + str2; // "Hello there" // a string and a character string literal string str1 = "Hello "; string str4 = str1 + "there"; //Also "Hello there" // a string and a single character string str5 = "The End"; string str6 = str5 + '!'; // "The End!"
- The plus-equals
+=
operator appends:
ie: it combines the assignment and concatenation operations in the way that you would expect.
The right-hand side must be a string object, a string literal, or a single character.string str1 = "Hello "; str1 += "there"; // "Hello there"