chunlanse2014

导航

The string Class(PART 2)

Working with Strings

So far, you’ve learned that you can create string objects in a variety of ways, display the
contents of a string object, read data into a string object, append(附加) to a string object,
assign(赋值) to a string object, and concatenate(连接) two string objects.What else can you do?
You can compare strings.All six relational operators(关系操作符) are overloaded for string objects,
with one object being considered less than another if it occurs earlier in the machine collating(校对)
sequence. If the machine collating sequence is the ASCII code, that implies that
digits are less than uppercase(大写字母的) characters and uppercase characters are less than lowercase
characters. Each relational operator is overloaded three ways so that you can compare a
string object with another string object, compare a string object with a C-style
string, and compare a C-style string with a string object:

1 string snake1("cobra");
2 string snake2("coral");
3 char snake3[20] = "anaconda";
4 if (snake1 < snake 2) // operator<(const string &, const string &)
5 ...
6 if (snake1 == snake3) // operator==(const string &, const char *)
7 ...
8 if (snake3 != snake2) // operator!=(const char *, const string &)
9 ...

You can determine the size of a string. Both the size() and length() member functions
return the number of characters in a string:

1 if (snake1.length() == snake2.size())
2 cout << "Both strings have the same length.\n"

Why two functions that do the same thing? The length() member comes from earlier
versions of the string class, and size() was added for STL compatibility(兼容性).
You can search a string for a given substring(子字符串 ) or character in a variety of ways.Table
16.2 provides a short description of four variations of a find() method. Recall that
string::npos is the maximum possible number of characters in a string, typically the
largest unsigned int or unsigned long value.

 

Table 16.2 The Overloaded find() Method

Method Prototype(原型)  Description
size_type find(const string &
str, size_type pos = 0) const
Finds the first occurrence of the substring str,
starting the search at location pos in the invoking(调用)
string. Returns the index of the first character of
the substring if found and returns string::npos
otherwise.
size_type find(const char * s,
size_type pos = 0) const
Finds the first occurrence of the substring s,
starting the search at location pos in the invoking
string. Returns the index of the first character of
the substring if found and returns string::npos
otherwise.
size_type find(const char * s,
size_type pos = 0, size_type n)
Finds the first occurrence of the substring
consisting of the first n characters in s, starting
the search at location pos in the invoking string.
Returns the index of the first character of the
substring if found and returns string::npos
otherwise.
size_type find(char ch, size_type
pos = 0) const
Finds the first occurrence of the character ch,
starting the search at location pos in the invoking
string. Returns the index of the character if found
and returns string::npos otherwise.
1 string name1("my name is apple");
2 string name2("is");
3 char name3[10]="app";
4 char ch='i';
5 int where1=name1.find(name2,0);
6 int where2=name1.find(name3,0);
7 int where3=name1.find(name3,0,2);
8 int where4=name1.find(ch,0);     

 

The string library also provides the related methods rfind(), find_first_of(),
find_last_of(), find_first_not_of(), and find_last_not_of(), each with the same
set of overloaded function signatures as the find() method.The rfind() method finds
the last occurrence of a substring or character.The find_first_of() method finds the
first occurrence in the invoking string of any of the characters in the argument. For
example, the following statement would return the location of the r in "cobra" (that is,
the index 3) because that’s the first occurrence of any of the letters in "hark" in "cobra":

1 int where = snake1.find_first_of("hark");

The find_last_of() method works the same, except it finds the last occurrence.
Thus, the following statement would return the location of the a in "cobra":

1 int where = snake1.find_last_of("hark");

The find_first_not_of() method finds the first character in the invoking string that
is not a character in the argument. So the following would return the location of the c in
cobra because c is not found in hark:

1 int where = snake1.find_first_not_of("hark");

 

 











 

posted on 2015-04-27 10:45  chunlanse2014  阅读(128)  评论(0编辑  收藏  举报