C++ Strings and Functions


C++ Strings

C++ has a string class. Some functions are defined in the class for strings to use. In C++ programming, you can declare string variables/objects. A string may be declared with or without an initial value. If you do not indicate the initial value, the initial value is an empty string (zero length, no characters).

Here are some examples to help you understand C++ strings:


	 //declare a string str1

	 string str1; 

	 //A string variable/object may be initialized with
	 //a character string literal:

	 string str2 = "Hello there";
	 string str3 ("Goodbye");  // Alternate form 

	 //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); 

The length( ) and size( ) Functions

Both of these functions return the number of characters in the string.

It returns 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 second character 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"
   

Operators

A number of C++ operators also work with strings.

 
	//Assigning one string's value to another string 
	string string_one = "Hello";
	string string_two;
	string_two = string_one; 

	//Assigning a single character (char) to a string
	string string_four;
	char ch = 'A';
	string_four = ch;
	string_four = 'Z'; 


	// 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"; 

	// a string and a single character
	string str5 = "The End";
	string str6 = str5 + '!'; 


	string str1 = "Hello ";
	str1 += "there"; 

Back to the top


Copyright: Department of Computer Science, University of Regina.