Return To CS 110 Home

Colour Theme   Font Size Options
 
   
   
   
Strings

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.

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.

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.