Strings

So far we have discussed C++'s handling of both integer and floating-point arithmetic, and we have talked about how variables can be used to save the results of numerical expressions in memory. However, programs often have to manipulate text data. For instance, a compiler analyzes program text, and a word processor must detect the ends of words and of sentences. String literals such as “this is a string of characters” are part of the C++ language. However, C++ does not directly support character string data types. The standard C++ library has defined a type string, which is simple and intuitive to use.

The type string

The type string is not built into C++ like int or float, but it is made possible by the class mechanism of C++. Standard strings are objects. However, these strings are so straightforward to use that programmers can use strings as if they were built in, without worrying about the details. In the past few years, the standard C++ library has become widely accepted and implemented, and this means that there is agreement on how strings should work and how to manipulate them. String variables may be declared like any other type, as in the following example:


;> string s = "hello dolly";
;> s;
(string) s = 'hello dolly'
;> s.find("dolly");
(int) 6
;> s.find("swell");
(unsigned long int) 4294967295

find() is a special kind of function: It is associated with an object by using the dot (.) operator. An object has an associated set of functions called methods, which you can use to modify the object and ask for its properties. In the preceding example, the find() method is used to find the position of the substring “dolly”, and it always returns the largest possible positive integer if it fails.

Concatenating Strings

A common operation on strings is to compose them out of smaller substrings. For example, the operator + has the effect of concatenating two strings:


;> string y = " you're so swell";
;> s
							+
							y;
(string) 'hello dolly you're so swell'
;> y
							+
							"
							dolly";
(string) 'you're so swell dolly'
;> s
							+= " so fine";
(string) 'hello dolly so fine'
;> s.length();
(int) 20

Similarly, the operator += appends a string to another string, modifying the target string. There is no operator because there is no C++ operation that is like subtraction on strings. At any point, you can find out the length of the string by using the method length(), which takes no arguments. In such a case, you put an empty list after the name.

Note that the first example uses a single quote character within the double quotes: “ you're so swell”.

How would you put a double quote in a string literal without prematurely ending the string? C++ allows you to embed special characters in a string, by using the backslash escape character () followed by a character. Note that you must double up the backslashes if you want to do a DOS-style pathname; otherwise, each in the pathname will be interpreted as an escape character. Other very useful escapes are (for return) and (for tab). In the following code, I have created three strings containing special characters. The last is printed out on three lines because it contains two return characters:


;> string path = "here is a "quote"";
;> path;
(string&) 'here is a "quote"'
;> path = "c:\programs\ucw";
(string&) 'c:programsucw'
;> path = "line1
line2
line3";
(string&) 'line1
line2
line3'

Finding and Extracting Substrings

The substr() method takes two arguments; the first is a start position, measured from zero, and the second is a character count. The substr() method copies the specified number of characters from the start position, and it is like the Borland Pascal copy function or the BASIC left$ function. In the example, I use substr() to extract substrings. The third call uses the index returned by find():


;> p
							=
							"hello dolly";
(string&) 'hello dolly'
;> p.substr(0,1);
(string) 'h'
;> p.substr(1,2);
(string) 'el'
;> p.substr(p.find("dolly"), 5);
(string) 'dolly'

As mentioned earlier in the chapter, the string's find() method looks for the first occurrence of a substring. It returns an index that substr() can use (that is, it is not negative). Generally, you should always check the return value before using it.

The replace() method takes a specified substring, like substr(), but it replaces the substring with the given string, rather than extracting it:

p.replace(0,5,"goodbye");
(string&) 'goodbye dolly'

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.191.176.5