5.2 Strings

I l @ ve RuBoard

C++ comes with an excellent string package. Unlike the numbers such as int and float , which are built-in to the core language, the std::string type comes from the C++ library. Before you can use this type, you must bring in the std::string definition with the statement:

 #include <string> 

After this you can declare strings like any other variable:

 std::string my_name; 

A string constant is any text enclosed in double quotes. So to assign the variable my_name the value "Steve", we use the statement:

 my_name = "Steve"; 

Strings may be concatenated using the + operator. Example 5-2 illustrates how to combine two strings (and a space) to turn a first and last name into a full name .

Example 5-2. string/string.cpp
 #include <string> #include <iostream> std::string first_name; // First name of the author std::string last_name;  // Last name of the author std::string full_name;  // Full name of the author int main(  ) {     first_name = "Steve";     last_name = "Oualline";     full_name = first_name + " " + last_name;     std::cout << "Full name is " << full_name << '\n';     return (0); } 

A string can be treated like an array of characters . Each character in the string can be accessed through the [ ] operation. For example:

 char first_ch;    // First character of the name ....     first_ch = first_name[0]; 

If the subscript is out of range, the result is undefined. That means that sometimes you'll get a random character, sometimes your program will crash, and sometimes something else will happen. You could protect things with an assert statement, which is described later in this chapter, but there's a better way to do it.

That is to access the characters through the at member function. (We'll learn all about what exactly a member function is in Chapter 13.) It works just like [] except that if the index is out of range, it throws an exception and shuts your program down in an orderly manner. See Chapter 22 for a description of exceptions.)

So to get the first character of our string safely, we need to use the statement:

 first_ch = first_name.at(0); 

The length of a C++ string can be obtained with the length member function. For example:

 std::cout << full_name << " is " << full_name.length(  ) << " long\n"; 

Finally, to extract a portion of a string, there is the substr member function. The general form of this function is:

   string   .substr(   first   ,   last   ) 

This function returns a string containing all the characters from first to last . For example, the following code assigns the variable sub_string the word "is":

 //             01234567890123 main_string = "This is a test"; sub_string = main_string.substr(5, 6); 

If first is out of range, an exception is generated and your program is terminated . If last is too big, the substr function returns as much of the string as it can get.

If you omit the last parameter, substr returns the rest of the string.

The std::string variable type has lots of useful and advanced features. For a full list, see your C++ documentation or help text.

5.2.1 Wide Strings

The wstring data type acts just like the string data type except that uses wide characters ( wchar_t ) instead of characters ( char ). Wide string constants look just like string constants, but they begin with L" instead of " . For example:

wstring name = L" figs/c++2_05_japan.gif ";

I l @ ve RuBoard


Practical C++ Programming
Practical C Programming, 3rd Edition
ISBN: 1565923065
EAN: 2147483647
Year: 2003
Pages: 364

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net