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.
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 substring 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);
.length()
and .size()
FunctionsBoth 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;
.find()
and .substr()
Functions.find()
function searches the string it
is called on to find the first occurance of a particular substring.
.find()
returns the position of the first character. .find()
returns the special value string::npos
.
.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.
.substr()
function always returns a substring of the string it is
called on.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"
A number of C++ operators also work with strings.
=
(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;
//Assigning a character array or string literal to a C++ string
//(You'll learn more about arrays and how they relate to strings later.)
char str[] = "I'm a character array!";
string string_three = str;
string_three = "I'm a string literal! My type is const char[] - a character array :O";
//Assigning a single character (char) to a string
string string_four;
char ch = 'A';
string_four = ch;
string_four = 'Z';
+
(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!"
+=
operator appends:
string str1 = "Hello ";
str1 += "there"; // "Hello there"
© Copyright: Department of Computer Science, University of Regina.