chunlanse2014

导航

< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

统计

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   chunlanse2014  阅读(130)  评论(0编辑  收藏  举报

(评论功能已被禁用)
编辑推荐:
· AI与.NET技术实操系列:基于图像分类模型对图像进行分类
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
阅读排行:
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· 25岁的心里话
· ollama系列01:轻松3步本地部署deepseek,普通电脑可用
· 按钮权限的设计及实现
点击右上角即可分享
微信分享提示